MATLAB: Reshaping and the size of an array

problem with nan in a matrix

Hi!
I have this statement in a program
% mag=reshape(mag,length(mag)/27,[]);
the size of mag is now 120*27
when i make this statement to clear nan values : (ids is an index of a condition!!!)
mag(ids)=nan;
mag=mag(~isnan(magg));
i get a size of m*1. (the length m is smaller than the number 120*27) why does the size of may matrix mag change when i use mag=mag(~isnan(magg))?
how could i get the n*27 (the length n is smaller than the number 120)
thank you
Hi It was my mistake the statement is: magg=mag,mag=mag(~isnan(mag))
i hope you could help me!

Best Answer

You do not post the contents of magg, but obviously it has less non-NaN elements than 120x27. Do you mean mag with one "g"?
If you remove NaN elements from an array, the array get smaller. This is evident, isn't it? And when elements disappear, it is impossible to guarantee, that the remaining number of elements allows to build an rectangular matrix with the same number of elements for all columns and the same for all rows. Therefore Matlab replies a vector for logical indexing.
Simply try it:
A = [NaN, 1; 2, 3]
A = A(~isnan(A))
What else instead of a [3 x 1] vector could be replied?
If you want to remove all rows, which contain any NaN:
index = all(~isnan(A), 2);
A = A(index, :);
Now keeping the same number of elements per row can and does work.