how to write an if statement for a matrix with some NaN elements?

9 Ansichten (letzte 30 Tage)
Mnr
Mnr am 5 Mai 2015
Bearbeitet: Stephen23 am 6 Mai 2015
Hello all,
I have a matrix with some elements 0s or 1s and the rest are NaNs. I would like to write an if statement that ignores the NaN elements of the matrix. I have tried
if (a(:)==1 or a(:)==0)
------------
end
however, I am getting an error. I would appreciate if somebody can help me please. Thanks.
  7 Kommentare
Stephen23
Stephen23 am 6 Mai 2015
Bearbeitet: Stephen23 am 6 Mai 2015
As Walter Roberson pointed out, it is very important to know that NaN is never equal to anything, not even itself:
>> NaN==NaN
ans =
0
The only way to test for NaN is using isnan, (or by implication isinf and isfinite):
Walter Roberson
Walter Roberson am 6 Mai 2015
~(A==A) is also a test for A being NaN.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 5 Mai 2015
nonnanlocs = ~isnan(a);
a(nonnanlocs) = SomeFunction(a(nonnanlocs));

Community Treasure Hunt

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

Start Hunting!

Translated by