Merge two matrices taking non-Null values

25 ビュー (過去 30 日間)
rosado
rosado 2019 年 2 月 14 日
回答済み: Stephen23 2024 年 4 月 4 日 18:02
I have two matrices containing essentially duplicate data, except some values are Null in one but not the other, I need to take the non-Null values from each and combine them into one matrix.
e.g. combine A and B to make C:
A = [8 8 8 8 NaN;
32 32 25 31 NaN;
56 56 43 53 NaN;
81 80 60 76 NaN;
105 103 78 99 NaN;
129 127 95 122 NaN]
B = [8 NaN 8 8 8;
32 NaN 25 31 31;
56 NaN 43 53 53;
81 NaN 60 76 76;
105 NaN 78 99 99;
129 NaN 95 122 121]
C = [8 8 8 8 8;
32 32 25 31 31;
56 56 43 53 53;
81 80 60 76 76;
105 103 78 99 99;
129 127 95 122 121]
Does anyone know if there's a way I can do this?

採用された回答

madhan ravi
madhan ravi 2019 年 2 月 14 日
C=zeros(size(A));
C(:,~all(isnan(A)))=A(:,~all(isnan(A)));
C(:,~all(isnan(B)))=B(:,~all(isnan(B)));
  1 件のコメント
Jose Oliveira
Jose Oliveira 2024 年 4 月 4 日 17:53
Very good!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2024 年 4 月 4 日 18:02
A = [8,8,8,8,NaN;32,32,25,31,NaN;56,56,43,53,NaN;81,80,60,76,NaN;105,103,78,99,NaN;129,127,95,122,NaN];
B = [8,NaN,8,8,8;32,NaN,25,31,31;56,NaN,43,53,53;81,NaN,60,76,76;105,NaN,78,99,99;129,NaN,95,122,121];
C = mode(cat(3,A,B),3)
C = 6x5
8 8 8 8 8 32 32 25 31 31 56 56 43 53 53 81 80 60 76 76 105 103 78 99 99 129 127 95 122 121
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by