Clear Filters
Clear Filters

How to copy or transfer a portion of matrix to another at same location?

2 views (last 30 days)
Hi, Is there a way to copy or transfer a portion of matrix A (nonzero values) to another matrix B at same location?
for example:
A = [ 0 0 0 0 0; 0 1 0 1 0; 0 1 0 1 0; 0 0 0 0 0 ];
B = [ 8 8 8 8 8; 8 8 8 8 8; 3 3 3 3 3; 2 2 2 2 2 ];
% desired answer is
C = [ 8 8 8 8 8; 8 1 8 1 8; 3 1 3 1 3; 2 2 2 2 2 ];

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 11 May 2016
index=A~=0;
C=B;
C(index)=A(index);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!