MATLAB Answers


5 views (last 30 days)
Johan on 14 Jun 2012
Commented: Walter Roberson on 6 Jan 2017
Hi! Is it possible to make NaN+double~=NaN instead of NaN+double=NaN? Johan


Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 14 Jun 2012
A = [nan 2 3 5 nan]
p = 5
out1 = A + p
way without NaN
A2 = A
A2(isnan(A2)) = 0
out2 = A2 + p


Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 14 Jun 2012
Well, technically speaking, there are 2^52 different NaN values, half of which (2^51) can be stored like NaN, and (with some effort) distinguished between. MATLAB tends to be careless about the different NaN values, tending to convert them to its one usual NaN, but you can work with them if you are careful.
Somehow I don't think this is what you are looking for, though.
If you want NaN+double to be the double, then don't do the addition operation: check for NaN first and code the result you want.
There is no way in MATLAB to tell MATLAB to handle NaN any differently than it already does. The result of operating on NaN is defined by the IEEE 754 standards on floating point numbers, and is built right in to the CPU.


Jan on 14 Jun 2012
The IEEE 754 handling of signaling or non-signaling NaNs is well established.
I would not invent new conventions for the NaN arithmetics, but catch the actual problem by another mechanism like a LOGICAL vector, which caries the information about the validity of the values. Therefore I agree with Walter. +1
Walter Roberson
Walter Roberson on 6 Jan 2017
You could consider nansum() which is also Also, in a recent release there is sum(X, 'omitnan')

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by