Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all NaN elements inside an Array

Tags:

nan

matlab

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

like image 624
Graviton Avatar asked Nov 11 '09 07:11

Graviton


1 Answers

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.

like image 170
Marc Avatar answered Sep 18 '22 15:09

Marc