NaN+double~=NaN
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi! Is it possible to make NaN+double~=NaN instead of NaN+double=NaN? Johan
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 6월 14일
eg
A = [nan 2 3 5 nan]
p = 5
out1 = A + p
way without NaN
A2 = A
A2(isnan(A2)) = 0
out2 = A2 + p
댓글 수: 0
추가 답변 (1개)
Walter Roberson
2012년 6월 14일
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
Jan
2012년 6월 14일
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
2017년 1월 6일
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')
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!