NaN+double~=NaN

5 vues (au cours des 30 derniers jours)
Johan
Johan le 14 Juin 2012
Hi! Is it possible to make NaN+double~=NaN instead of NaN+double=NaN? Johan

Réponse acceptée

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

Plus de réponses (1)

Walter Roberson
Walter Roberson le 14 Juin 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.
  2 commentaires
Jan
Jan le 14 Juin 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 le 6 Jan 2017
You could consider nansum() which is also https://www.mathworks.com/help/finance/nansum.html. Also, in a recent release there is sum(X, 'omitnan')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by