MATLAB Answers

substituting value in matrix does not work for some reason

2 views (last 30 days)
Bogdan
Bogdan on 18 Nov 2014
Edited: Kelly Kearney on 18 Nov 2014
A = [6 9 NaN NaN 8 NaN]; B = [2 5 342 232 1 116];
sizeA = size(A);
for m = [1:1:size(2)];
if A(1,m) == NaN;
B(1,m) = NaN;
end;
end;
I was hoping B would become [2 5 NaN NaN 8 NaN], but it did not change. I would appreciate any help

  0 Comments

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 18 Nov 2014
Edited: Kelly Kearney on 18 Nov 2014
You can't use == with NaNs:
>> NaN == NaN
ans =
0
Use isnan instead:
B(isnan(A)) = NaN;

  0 Comments

Sign in to comment.

More Answers (1)

Kevin Claytor
Kevin Claytor on 18 Nov 2014
Logical indexing makes this much easier;
B(A==NaN) = NaN;

  0 Comments

Sign in to comment.

Sign in to answer this question.