Superimpose matrices of different sizes

2 ビュー (過去 30 日間)
Dave
Dave 2014 年 12 月 19 日
コメント済み: Dave 2014 年 12 月 19 日
Hello,
I have matrix A 2x5 of NaN
A=...
[NaN NaN NaN NaN NaN;
NaN NaN NaN NaN NaN]
and another B matrix 2x3:
B=...
[NaN 6 2;
NaN 1 0]
If the elements of B are postive (so not 0 nor NaN) I want to paste those elements into A, call this result matrix C
C=...
[NaN 6 2 NaN NaN;
NaN 1 NaN NaN NaN]
Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 19 日
編集済み: Azzi Abdelmalek 2014 年 12 月 19 日
EDIT
C=A
B(B==0)=nan
C(1:size(B,1),1:size(B,2))=B
%or
A=nan(2,5)
B=[NaN 6 2;NaN 1 0]
x=B>0;
[ii,jj]=find(x)
idx=sub2ind(size(A),ii,jj)
C=A;
C(idx)=B(x)
  2 件のコメント
Thorsten
Thorsten 2014 年 12 月 19 日
編集済み: Thorsten 2014 年 12 月 19 日
This does not work for the entry in B that is 0, which should not be copied.
Dave
Dave 2014 年 12 月 19 日
Thanks Azzi and Thorsten, not allowed to accept both answers here. yeah, doesn't consider the 0 case (before the EDIT changes), but can do C(C==0)=NaN afterwards.

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

その他の回答 (1 件)

Thorsten
Thorsten 2014 年 12 月 19 日
編集済み: Thorsten 2014 年 12 月 19 日
This works only if A and B have the same number of rows:
ind = find(B>0);
C = A;
C(ind) = B(ind);

カテゴリ

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