Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

convert zeros to nan

Asked by Hassan on 22 Apr 2011

I have an array (10 rows,10 columns,5 bands) and wonder how I can convert the zero values to NaN. I used the following command line but it's not working. If I replace zero values with another value (like 2) it works but for an odd reason is not working with NaN. A(A==0)=NaN

I apperciate your help.





No products are associated with this question.

4 Answers

Answer by John D'Errico on 22 Apr 2011
Accepted answer

Given that your array is truly uint32, try this:

ans =

NaNs are only defined in context of FLOATING point numbers.

1 Comment

Hassan on 22 Apr 2011

I tried that. I got the same answer (ans=0).

John D'Errico
Answer by John D'Errico on 22 Apr 2011

It DOES work.

A = [1 2 0 -4 5 0 0 6];
A(A == 0) = NaN
A =
     1     2   NaN    -4     5   NaN   NaN     6

So you are mistaken that it fails. Very likely, you are failing to understand that matlab sometimes displays a number as 0, yet it is NOT zero.

format short
A = [1 2 0.00000001 -4 5 0.000000023 0.000000000001 6]
A =
    1.0000    2.0000    0.0000   -4.0000    5.0000    0.0000    0.0000    6.0000

See that there are still three values that are displayed as zero, but the simple test for zero fails to see any of them

A == 0
ans =
     0     0     0     0     0     0     0     0

I will also point out that you CANNOT test for a NaN using ==, as that test will always return false. This is easy to prove.

nan == nan
ans =

The final possibility is that you have defined nan to be some other value. Thus

nan = 5;

Now I will not be able to assign something as a true nan, instead, matlab will use 5 when you try that. So, if you have defined nan = 0 someplace, then replacing zeros by nan will just insert zeros directly back in.


John D'Errico on 22 Apr 2011

I think Sean has the answer. Are you using some other class? If you don't give us sufficient information, we are just guessing here.

John D'Errico on 22 Apr 2011

Nan is not defined as a valid value for uint32 numbers.

Hassan on 22 Apr 2011

OK, in that case I need to use other values instead of NaN. thanks John for the help.

John D'Errico
Answer by Matt Fig on 22 Apr 2011

Please do these three commands on your machine and paste the output, just like I did. Remember to use the {} Code button!

>> which nan
built-in (C:\Program Files\MATLAB\R2007b\toolbox\matlab\elmat\nan)
>> B = mod(1:5,2)
B =
     1     0     1     0     1
>> B(~B)=nan
B =
     1   NaN     1   NaN     1



Hassan, don't put in the >> when you run the code.


Hassan on 22 Apr 2011

thanks Matt for the comment. it gives me the following error:
??? built-in (C:\Program Files\MATLAB\R2007b\toolbox\matlab\elmat\nan)
Error: Unexpected MATLAB operator.

Matt Fig on 22 Apr 2011

I hope you didn't try to put in the >> when you did the commands, did you?? I only left them there to show that I did this at the command window!

Hassan on 22 Apr 2011

No I didn't. I am using Matlab 2009b version on the university network. i couldn't find Matlab folder in Program Files.

Matt Fig
Answer by Ali Can ARIK on 22 Apr 2011

Try this:

A(find(A==0)) = NaN;


John D'Errico on 22 Apr 2011

What does it do when you try that? Have you possibly defined a variable nan to have some other value?

Hassan on 22 Apr 2011

I clear all variables using 'clear all' command. it can't convert zeros to nan for no reason.

Hassan on 22 Apr 2011

when I convert a value to NaN, it is converted to zero.

Ali Can ARIK

Contact us