Given an n-by-n matrix A with elements equal to either 0 or 1, I want to replace only nonzero elements (A_ij)'s by (b_ij) and leave 0's intact where (b_ij)'s are themselves elements of another n-by-n matrix B. I do it as following
a[A_, n_] :=
Table[If[A[[i, j]] == 1, A[[i, j]] = B[[i, j]], 0], {i,
n}, {j, n}]
Then by setting n=5, I get the following errors:
Part::partd: Part specification amat[[1,2]] is longer than depth of object. Set::setps: {{0,1,0,0,0},{1,0,1,0,0},{0,1,0,1,0},{0,0,1,0,1},{0,0,0,1,0}} in the part assignment is not a symbol.
How can I get rid of these errors? Is there a better way to do this procedure?
Thanks!
newMatrix = A B
$\endgroup$