Given the following matrix:
m = Array[Subscript[a, #, #2] &, {4, 4}]
how can I find the skew diagonal or anti-diagonal or back diagonal of the matrix (shown in red)
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 communityMore options:
a = Range@12 ~Partition~ 4;
a // MatrixForm
$\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{array} \right)$
Diagonal[Reverse @ a]
{9, 6, 3}
Diagonal[a ~Reverse~ 2, 1]
{3, 6, 9}
Diagonal[a ~Reverse~ 2]
{4, 7, 10}
Diagonal[Reverse @ a, 1]
{10, 7, 4}
Diagonal[Reverse /@ Array[f, {4, 4}]]
{f[1, 4], f[2, 3], f[3, 2], f[4, 1]}
Diagonal[]
.
$\endgroup$
Apr 14, 2013 at 1:39
I believe you can take diagonal off a rectangular matrix too. "Back 'diagonal'".
SeedRandom[1];
m = RandomInteger[6, {3, 4}]
(* {{6, 4, 2, 4}, {0, 1, 6, 0}, {0, 2, 0, 6}} *)
Table[m[[i, -i]], {i, Min@Dimensions@m}]
(* {4, 6, 2} *)
Here's a flexible version:
antidiag[m_] := Diagonal[m[[-1 ;; 1 ;; -1, 1 ;; -1 ;; 1]]]
antidiag[m_, offset_] :=
Diagonal[m[[offset ;; -1, offset ;; -1]][[-1 ;; 1 ;; -1,
1 ;; -1 ;; 1]]]
with no second argument, it gives what you want.
MatrixForm[m = Array[Subscript[a, #1, #2] &, {5, 5}]]
antidiag[m]
antidiag[m, 2]
Reverse
.
$\endgroup$
Apr 13, 2013 at 23:01
If by "back diagonal", you mean the diagonal from the NE corner to the SW corner of the matrix, then you can obtain it the following way:
Clear@AntiDiagonal
AntiDiagonal[m_?MatrixQ] /; Equal @@ Dimensions@m :=
Composition[Reverse, Diagonal, Reverse][m]
Reverse
to the matrix beforeDiagonal
. The elements will be in reverse order so you might want to applyReverse
again to the result... $\endgroup$MapThread[Part, {Array[C, {5, 5}], -Range[5]}]
. $\endgroup$