remove nans from a matrix

  • Follow


Hi, 
is there a short way to remove nans from a matrix or do I have to loop through the elements using isnan. Here is the case: I have two matrices coups and mats which contains nans. I have written this code that loops through each element in the two matricces in order to remove nans. The problem is that it gives me the following error message:
??? Subscripted assignment dimension mismatch.

Here is the code:
for ii=1:length(coups(:,1));
    for dd = 1:length(coups(1,:));
        if isnan(coups(ii,dd)) && isnan(mats(ii,dd));
            coups(ii,dd)=[];mats(ii,dd)=[];
        end;
    end;
end;

Does anyone has any ideas what might be wrong?

Hanna
0
Reply Hanna 12/17/2009 3:04:08 PM

On Dec 17, 4:04=A0pm, "Hanna " <hann...@kth.se> wrote:
> Hi,
> is there a short way to remove nans from a matrix or do I have to loop th=
rough the elements using isnan. Here is the case: I have two matrices coups=
 and mats which contains nans. I have written this code that loops through =
each element in the two matricces in order to remove nans. The problem is t=
hat it gives me the following error message:
> ??? Subscripted assignment dimension mismatch.
>
> Here is the code:
> for ii=3D1:length(coups(:,1));
> =A0 =A0 for dd =3D 1:length(coups(1,:));
> =A0 =A0 =A0 =A0 if isnan(coups(ii,dd)) && isnan(mats(ii,dd));
> =A0 =A0 =A0 =A0 =A0 =A0 coups(ii,dd)=3D[];mats(ii,dd)=3D[];
> =A0 =A0 =A0 =A0 end;
> =A0 =A0 end;
> end;
>
> Does anyone has any ideas what might be wrong?
>
> Hanna

you could just use, coups(isnan(coups)) =3D []

but, the resultant will be a vector, not a matrix anymore. That is why
I think you are getting the error. The first time you replace it
becomes a vector and then you can't access using ii and jj.
0
Reply arun 12/17/2009 3:40:15 PM


Dear Hanna!

> is there a short way to remove nans from a matrix or do I have to loop through the elements using isnan. Here is the case: I have two matrices coups and mats which contains nans. I have written this code that loops through each element in the two matricces in order to remove nans. The problem is that it gives me the following error message:
> ??? Subscripted assignment dimension mismatch.
> 
> Here is the code:
> for ii=1:length(coups(:,1));
>     for dd = 1:length(coups(1,:));
>         if isnan(coups(ii,dd)) && isnan(mats(ii,dd));
>             coups(ii,dd)=[];mats(ii,dd)=[];
>         end;
>     end;
> end;

Similar to your posting again:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/268542#702858
If you delete elements from a matrix, this matrix becomes smaller and a vector! You cannot have "vanishing" elements in a matrix. But if your matrix shrinks, you cannot access the final elements, because they are not there anymore => subscripted assignment dimension mismatch.

Just try it manually:
  M = [1, 2; 3, 4];
  M(1, 2) = [];  % delete one element
What do you expect now? What dimensions has M? 

I've mentioned already - except that you wanted ~isnan(mats(ii,dd)) the last time:
  both_isnan = isnan(coups) & isnan(mats);
  coups(both_isnan) = [];
  mats(both_isnan) = [];
But then coups and mats are not matrices anymore - usually.

Kind regards, Jan
0
Reply Jan 12/17/2009 5:43:22 PM

2 Replies
248 Views

(page loaded in 0.024 seconds)


Reply: