7
$\begingroup$

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.

$\endgroup$
2
  • 4
    $\begingroup$ Try Transpose@Reverse[m, {1, 2}]. $\endgroup$
    – Szabolcs
    Dec 17, 2014 at 16:38
  • 2
    $\begingroup$ Transpose@m[[-1 ;; 1 ;; -1, -1 ;; 1 ;; -1]] is faster for big numerical arrays because of the perforamce bug in Reverse $\endgroup$
    – ybeltukov
    Dec 17, 2014 at 20:19

2 Answers 2

5
$\begingroup$
mA = 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]}}

$\endgroup$
1
$\begingroup$
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) $

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.