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

matrix manipulation

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 ];

Best Answer

index=A~=0;
C=B;
C(index)=A(index);