MATLAB: A(a==NaN) does not find NaN’s in matrix. What to do

MATLABnan

I have
a=1 NaN 2 6
I want to change the NaN to 0 but
a(a==NaN)=0
does not work and gives me
a =
1.00 NaN 2.00 6.00
What did i do wrong?

Best Answer

NaNs are not equal to themselves!
You could thus do either of these:
* a(isnan(a))=0; %isnan
* a(a~=a) = 0; %nans aren't equal to themselves