When comparing values to NaN
using the relational equals
operator (==
), MATLAB always returns
false
. For example, the following code displays
false
even though it compares NaN
to
NaN
:
x = NaN;
if x == NaN
disp('true')
else
disp('false')
end
Instead of using the relational equals operator to compare a variable to
NaN
, use isnan
. For example, the following code displays
true
:
x = NaN;
if isnan(x)
disp('true')
else
disp('false')
end