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.

댓글 수: 1

Marina
Marina 2024년 2월 9일
Try the standardizeMissing function.

댓글을 달려면 로그인하십시오.

 채택된 답변

John D'Errico
John D'Errico 2011년 4월 22일

1 개 추천

Given that your array is truly uint32, try this:
clear
uint32(nan)
ans =
0
NaNs are only defined in context of FLOATING point numbers.

댓글 수: 1

Hassan
Hassan 2011년 4월 22일
I tried that. I got the same answer (ans=0).

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

John D'Errico
John D'Errico 2011년 4월 22일

15 개 추천

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 =
0
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.

댓글 수: 7

Hassan
Hassan 2011년 4월 22일
thanks John for the comment. when I do A==0 then it reconizes zeros. when I convert zero values to other values, the conversion works fine. and nan value is NaN and not other values as I check it. I don't know what the problem is.
John D'Errico
John D'Errico 2011년 4월 22일
You have said ONLY that it does not work for you. You have NOT said what it does when you try to do this though. Show an example where it fails.
Sean de Wolski
Sean de Wolski 2011년 4월 22일
What class is your data?
>> class(A)
Nan is not defined for all classes.
Hassan
Hassan 2011년 4월 22일
class(A) is unit32. it works when I creat an array with zero values but not for my data. my data is big and I can not show them here but can send you by email. using A(A==0)=2 it finds zero values and convert them to 2 and using A(A==2)=nan it finds 2 values but convert them to zero.
John D'Errico
John D'Errico 2011년 4월 22일
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
John D'Errico 2011년 4월 22일
Nan is not defined as a valid value for uint32 numbers.
Hassan
Hassan 2011년 4월 22일
OK, in that case I need to use other values instead of NaN. thanks John for the help.

댓글을 달려면 로그인하십시오.

Ali Can ARIK
Ali Can ARIK 2011년 4월 22일

1 개 추천

Try this:
A(find(A==0)) = NaN;

댓글 수: 4

Hassan
Hassan 2011년 4월 22일
thanks Ali for the reply. I tried that but it's not working. if I use A(A==0)=NaN or A(find(A==0))=NaN, the zero values stay zero. I can change the zero values to another value like 2 but I can not convert them to NaN.
John D'Errico
John D'Errico 2011년 4월 22일
What does it do when you try that? Have you possibly defined a variable nan to have some other value?
Hassan
Hassan 2011년 4월 22일
I clear all variables using 'clear all' command. it can't convert zeros to nan for no reason.
Hassan
Hassan 2011년 4월 22일
when I convert a value to NaN, it is converted to zero.

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 4월 22일

1 개 추천

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
.
. EDIT
Hassan, don't put in the >> when you run the code.

댓글 수: 3

Hassan
Hassan 2011년 4월 22일
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
Matt Fig 2011년 4월 22일
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
Hassan 2011년 4월 22일
No I didn't. I am using Matlab 2009b version on the university network. i couldn't find Matlab folder in Program Files.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2011년 4월 22일

댓글:

2024년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by