NaN+double~=NaN

2 visualizaciones (últimos 30 días)
Johan
Johan el 14 de Jun. de 2012
Comentada: Walter Roberson el 6 de En. de 2017
Hi! Is it possible to make NaN+double~=NaN instead of NaN+double=NaN? Johan

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 14 de Jun. de 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

Más respuestas (1)

Walter Roberson
Walter Roberson el 14 de Jun. de 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 comentarios
Jan
Jan el 14 de Jun. de 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 el 6 de En. de 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')

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by