I am new to Mathematica. A quick question:
Given
a = {{1,0,0,2},{2,0,1,0}}
aR = {{1,0,1,0},{0,1,0,1}}
if element in a
is 0
, I would like to replace this element with the element from aR
in the same position, so results should be `
{{1, 0, 1, 2}, {2, 1, 1, 1}}
.
I tried this but does not work
(a[[#1, #2]] = If [a[[#1, #2]] == 0, aR[[#2, #1]], a[[#1, #2]]]) & @@@
{Range[Part[Dimensions[a], 1]], Range[Part[Dimensions[a], 2]]};
Could you please help me?
a + (1 - Unitize@a)*aR
... and perhaps more understandableReplacePart[a, {x_, y_} /; a[[x, y]] == 0 :> aR[[x, y]]]
orMapThread[If[# == 0, #2, #] &, {a, aR}, 2]
$\endgroup$MapIndexed[If[#1 == 0, Extract[aR, #2], #1] &, a, {2}]
$\endgroup$