I need help with writing the mathematica code to reflect the entries of a square matrix about its anti-diagonal. I know to do it about the main diagonal using Transpose
.
What should be the precise code?
Thanks.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitymA = Array[a, {4, 4}]
{{a[1, 1], a[1, 2], a[1, 3], a[1, 4]},
{a[2, 1], a[2, 2], a[2, 3], a[2, 4]},
{a[3, 1], a[3, 2], a[3, 3], a[3, 4]},
{a[4, 1], a[4, 2], a[4, 3], a[4, 4]}}
mB = Reverse /@ (Transpose[Reverse /@ mA])
{{a[4, 4], a[3, 4], a[2, 4], a[1, 4]},
{a[4, 3], a[3, 3], a[2, 3], a[1, 3]},
{a[4, 2], a[3, 2], a[2, 2], a[1, 2]},
{a[4, 1], a[3, 1], a[2, 1], a[1, 1]}}
rF = With[{m = #, dim = Length@#}, Array[m[[dim + 1 - #2, dim + 1 - #1]] &, {dim, dim}]] &;
mA = Array[a, {4, 4}];
Row[MatrixForm /@ {mA, rF@mA}]
$\left( \begin{array}{cccc} a[1,1] & a[1,2] & a[1,3] & a[1,4] \\ a[2,1] & a[2,2] & a[2,3) & a[2,4] \\ a[3,1] & a[3,2] & a[3,3) & a[3,4] \\ a[4,1] & a[4,2] & a[4,3) & a[4,4] \\ \end{array} \right)\left( \begin{array}{cccc} a[4,4]& a[3,4]& a[2,4]& a[1,4]\\ a[4,3]& a[3,3]& a[2,3]& a[1,3]\\ a[4,2]& a[3,2]& a[2,2]& a[1,2]\\ a[4,1]& a[3,1]& a[2,1]& a[1,1]\\ \end{array} \right)$
aA = RandomInteger[9, {5, 5}];
Row[MatrixForm /@ {aA, rF@aA}]
$ \left( \begin{array}{ccccc} 3 & 6 & 4 & 0 & 9 \\ 5 & 3 & 3 & 4 & 1 \\ 3 & 7 & 4 & 8 & 2 \\ 1 & 9 & 7 & 7 & 2 \\ 0 & 4 & 1 & 8 & 2 \\ \end{array} \right)\left( \begin{array}{ccccc} 2 & 2 & 2 & 1 & 9 \\ 8 & 7 & 8 & 4 & 0 \\ 1 & 7 & 4 & 3 & 4 \\ 4 & 9 & 7 & 3 & 6 \\ 0 & 1 & 3 & 5 & 3 \\ \end{array} \right) $
Transpose@Reverse[m, {1, 2}]
. $\endgroup$Transpose@m[[-1 ;; 1 ;; -1, -1 ;; 1 ;; -1]]
is faster for big numerical arrays because of the perforamce bug inReverse
$\endgroup$