Clear Filters
Clear Filters

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

1 view (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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!