MATLAB: Superimpose matrices of different sizes

superimpose matrix different sizes

Hello,
I have matrix A 2×5 of NaN
A=...
[NaN NaN NaN NaN NaN;
NaN NaN NaN NaN NaN]
and another B matrix 2×3:
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

Best Answer

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)