How to force nansum(NaN+ NaN) = NaN, not 0 ?

5 ビュー (過去 30 日間)
Alex Kurek
Alex Kurek 2016 年 1 月 5 日
編集済み: Stephen23 2016 年 1 月 5 日
Hello,
I need to sum some multidimentional arrays after SPMD processing. This arrays contains also NaNs, but I need to computer a proper mean value of them later. Since:
>> nansum(NaN + NaN)
ans =
0
This case will affect the result. How to perform such a summing, but sum of ONLY NaNs has to be equal to NaN (not zero!)?
Best regards, Alex

採用された回答

Stephen23
Stephen23 2016 年 1 月 5 日
編集済み: Stephen23 2016 年 1 月 5 日
Just keep track of where all elements are NaN, and replace their sum with NaN after using nansum. You should be able to adapt this example to help you, it sums along each column:
>> A = [1,2,NaN,NaN;3,NaN,4,NaN]
A =
1 2 NaN NaN
3 NaN 4 NaN
>> B = [6,7,8,NaN;9,NaN,NaN,NaN]
B =
6 7 8 NaN
9 NaN NaN NaN
>> C = nansum(A+B,1)
C =
19 9 0 0
>> C(all(isnan(A)&isnan(B),1)) = NaN
C =
19 9 0 NaN

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by