tags:

views:

1805

answers:

3

Is there a command in MATLAB that allows me to find all NaN (Not-a-Number) elements inside an array?

+1  A: 

I just found the answer:

k=find(isnan(yourarray))

k will be a list of NaN element indicies.

Ngu Soon Hui
In Matlab you often does not need calling find() on logical array because logical arrays can be used directly instead of indices for most further operations, see for example http://blogs.mathworks.com/loren/2006/08/09/essence-of-indexing/
Mikhail
There are times when you just need the logical array, and there are times when you need the indices. I agree not to do unneeded operations, however the OP did not make it clear which he wanted. This answer is correct.
MatlabDoug
+7  A: 

While isnan is the correct solution, I'll just point out the way to have found it. Use lookfor. When you don't know the name of a function in MATLAB, try lookfor.

lookfor nan

will quickly give you the names of some functions that work with NaNs, as well as giving you the first line of their help blocks. Here, it would have listed (among other things)

ISNAN True for Not-a-Number.

which is clearly the function you want to use.

woodchips
+1 for teaching a man how to fish!
Marc
+7  A: 

As noted, the best answer is isnan() (though +1 for woodchips' meta-answer). A more complete example of how to use it with logical indexing:

>> a = [1 nan;nan 2]

a =

  1   NaN
NaN     2

>> %replace nan's with 0's
>> a(isnan(a))=0

a =

 1     0
 0     2

isnan(a) returns a logical array, an array of true & false the same size as a, with "true" every place there is a nan, which can be used to index into a.

Marc
I accepted your answer because you show me how to replace the NaN with 0 value, which is invaluable for my purpose.
Ngu Soon Hui

related questions