Swap two entries in a matrix
ReplaceAll
should do it:
{{2, 4, 1}, {3, 1, 2}, {4, 3, 1}} /. {2 -> 3, 3 -> 2}
A minor point regarding ReplaceAll
: ReplaceAll
does not change the matrix a
, so you do need to set a= (a/. {replacement rules})
or a=ReplaceAll[a,{replacement rules}]
.
a = {{2, 4, 1}, {3, 1, 2}, {4, 3, 1}};
a = (a /. {2 -> 3, 3 -> 2});
a // MatrixForm
Another alternative is to use Replace
at level 2:
a = Replace[a, {2 -> 3, 3 -> 2}, {2}]
or at level -1:
a = Replace[a, {2 -> 3, 3 -> 2}, {-1}]
Yet another alternative:
a = Cases[a, {x___, aa : (3 | 2), y___} :> {x, If[aa == 3, 2, 3], y}, {1,Infinity}]
Update: functions to swap a pair of elements of a matrix:
ClearAll[swap];
SetAttributes[swap, HoldFirst];
swap[mat_, elem1_, elem2_] := (mat = mat /. {elem1 -> elem2, elem2 -> elem1})
swap[a,2,3]
(* {{3, 4, 1}, {2, 1, 3}, {4, 2, 1}} *)
Swap multiple pairs of elements:
ClearAll[swap2];
SetAttributes[swap2, HoldFirst];
swap2[mat_, pairs:{{_, _} ..}] := (mat = mat /.(Rule @@@ Join[pairs, Reverse /@ pairs]))
swap2[a,{{2,3}}]
(* {{2, 4, 1}, {3, 1, 2}, {4, 3, 1}} *)
swap2[a,{{2,3},{1,4}}]
(* {{3, 1, 4}, {2, 4, 3}, {1, 2, 4}} *)
For large matrices and many pairs to swap:
ClearAll[swap3];
SetAttributes[swap3, HoldFirst];
swap3[mat_, pairs : {{_, _} ..}] := With[{dispatch = Dispatch[Rule @@@ Join[pairs, Reverse /@ pairs]]}, mat = (mat /. dispatch)]
I like the above ReplaceAll
solution the most. But to make sure there are at least 10 different ways to do something in Mathematica, here is another way
mat = {{2, 4, 1}, {3, 1, 2}, {4, 3, 1}};
pos1 = Position[mat, 2];
pos2 = Position[mat, 3];
mat = ReplacePart[mat, pos1 -> 3];
mat = ReplacePart[mat, pos2 -> 2]