When comparing values to NaN
, MATLAB always returns
false
. For example, the following code displays
other value
even though it compares
NaN
to NaN
:
x = NaN;
switch x
case -1
disp('negative one');
case 0
disp('zero');
case 1
disp('positive one');
case NaN
disp('NaN');
otherwise
disp('other value')
end
Rewrite your code so it does not compare a variable to NaN
.
For example, you can rewrite the previous code like this, using isnan
to determine if
the variable is NaN
:
x = NaN;
switch x
case -1
disp('negative one');
case 0
disp('zero');
case 1
disp('positive one');
otherwise
if isnan(x)
disp('NaN')
else
disp('other value')
end
end