0

I've been given some undocumented Matlab code, and trying to figure out what it does. I've put my main questions below as comments interspersed in the code.

% x is an array, I believe of dimension (R, 2)
% I understand that the following line creates a logical array y of the same 
% dimensions as x in which every position that has a number (i.e. not Nan) 
% contains True.
y=~isnan(x)

for k=1:R
   % I don't know if z has been previously defined. Can't find it anywhere 
   % in the code I've been given. I understand that z is a cell array. The
   % logical array indexing of x, I understand basically looks at each row
   % of x and creates a 1-column array (I think) of numbers that are not Nan,
   % inserting this 1-column array in the kth position in the cell array z.
   z{k}=x(k, y(k,:))
end
% MAIN QUESTION HERE: I don't know what the following two lines do. what 
% will 'n' and 'm' end up as? (i.e. what dimensions are 'd'?) 
d=[z{:,:}]
[m,n]=size(d)

1 Answer 1

3

About y=~isnan(x), you are right.

The line with x(k,y(k,:)) will give the not-Nans in the k-th row of x. So it seems that z is gathering the not-Nans values of x (in a weird way). Note that y(k,:) acts as a logical index for the columns, where true means "include that column" and false means "do not include".

As for your last question: [z{:,:}] is in this case equivalent to [z{:}], because z has one dimension only, and it will horizontally concatenate the contents of the cell array z. For example, with z{1} = [1; 2]; z{2} = [3 4; 5 6]; it will give [1 3 4; 2 5 6]. Thus m will be the common number of rows in the matrices that make up z, and n will be the sum of the numbers of columns (in my example m will be 2 and n will be 3). If there is no such common number of rows it will give an error. For example, if z{1} = [1 2]; z{2} = [3 4; 5 6]; then [z{:}] or [z{:,:}] give an error.

So the final result d is just a row vector which contains the not-Nans from x ordered by increasing row and then increasing column. That could have been obtained more easily as

xt = x.';
d = xt(~isnan(xt(:))).';

which is more compact, more Matlab-like, and probably faster.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, I made an error typing the line with x(k, (k:)). It should be x(k, z(k:)). I corrected that in the code. The horizontal concatenation, together with indexing by a logical array (in the line I corrected) makes sense as somehow selecting and concatenating non-NaN numbers, but I still am not sure what size/dimension arrays z will hold (that is, what size/dimension arrays are created by indexing by a logical array). Also I think you reversed the meaning of n & m; note how the line is [m,n] = size(d), not [n,m] = size(d).
You're right about m and n. And with your correction the code makes more sense. Please see updated answer.
The code obtains the result (d) in a weird way. It's not the standard way to do things in Matlab. Loops can be easily avoided here, the cell array is definitely not needed, and why work by rows? May I ask where you got it from? Some kind of automatically generated or translated code?
Got it from a medical doctor who hacked together code from different sources to help with his research into diagnosing certain conditions by speech/voice analysis. He barely understands programming. I don't think it's likely he could have written ANY of this from scratch; more likely he cobbled it together. Eventually I'll rewrite it in Python/numpy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.