How to use ISNAN function?

82 ビュー (過去 30 日間)
Shashank
Shashank 2012 年 9 月 27 日
How to write:
if A(i+2,11) == NaN % Originally posted as: if (i+2,11) == NaN
using ISNAN function?

回答 (1 件)

Matt Fig
Matt Fig 2012 年 9 月 27 日
編集済み: Matt Fig 2012 年 9 月 27 日
A = [3 nan 4];
isnan(A)
Are you wanting to check whether i+2 or 11 is a nan? Well we know that 11 is not a nan, so let's leave that out. If 'i' is not a nan then i+2 is not a nan. Thus we only have to check on 'i'. Now is 'i' the imaginary unit or are you masking this important MATLAB function by using 'i' as a loop index or other variable? If the latter, then
if isnan(i),disp('i is a nan'),end
By the way, IF statements in MATLAB pass the conditional if ALL of the elements evaluate to true. As you have it, that IF statement would never pass, no matter what 'i' is, because 11 is never a nan. I assume you meant something like:
if [i+2,11] == NaN,disp('In if'),end
  3 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 27 日
編集済み: Matt Fig 2012 年 9 月 27 日
I see, you left out the A in your code (you posted (i+2,11) and not A(i+2,11))!! You said "any real number" so I use 0:
if isnan(A(i+2,11))
A(i+2,11) = 0
end
Is it only this particular element you want to deal with, or all nans in the array A?
Shashank
Shashank 2012 年 9 月 27 日
Thanks a ton! I think I was making a mistake writing isnan inside the matrix.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by