Choose role of NaN when summing two matrices

2 ビュー (過去 30 日間)
Roderick
Roderick 2024 年 9 月 28 日
回答済み: Voss 2024 年 9 月 28 日
Dear all
Some time ago, I posted this question: Choosing the role of NaN elements in the sum environment of matrices. That worked great when doing the mean value, but now imagine that I just want to sum the two matrices, given by:
aa=[1 2; 3 NaN];
bb=[NaN 1; 2 NaN];
so that I obtain:
cc=[1 3; 5 NaN];
If I do something like:
cc=sum(cat(3,aa,bb),3,'omitnan')
that gives:
cc=[1 3; 5 0];
Which way would be the best in this case?

採用された回答

Voss
Voss 2024 年 9 月 28 日
Here's one way:
aa=[1 2; 3 NaN];
bb=[NaN 1; 2 NaN];
tmp = cat(3,aa,bb);
cc=sum(tmp,3,'omitnan');
cc(all(isnan(tmp),3)) = NaN
cc = 2×2
1 3 5 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by