开发者

Find all NaN elements inside an Array

开发者 https://www.devze.com 2022-12-10 22:22 出处:网络
Is there a command in MATLAB that allows开发者_开发知识库 me to find all NaN (Not-a-Number) elements inside an array?As noted, the best answer is isnan() (though +1 for woodchips\' meta-answer).A more

Is there a command in MATLAB that allows开发者_开发知识库 me to find all NaN (Not-a-Number) elements inside an array?


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.


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.


I just found the answer:

k=find(isnan(yourarray))

k will be a list of NaN element indicies.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号