Borrar filtros
Borrar filtros

a(a==NaN) does not find NaN's in matrix. What to do?

7 visualizaciones (últimos 30 días)
Sharon
Sharon el 29 de Mayo de 2012
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?

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 29 de Mayo de 2012
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

Más respuestas (1)

Thomas
Thomas el 29 de Mayo de 2012
Sharon, you are unable to replace the NaN because
NaN ~= NaN (in matlab NaN's are unequal) you can check this by
isequal(NaN,NaN)
ans =
0
to replace NaN's use the isnan function
doc isnan
For your example you need
a(isnan(a))=0
a =
1.00 0 2.00 6.00

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by