13
$\begingroup$

Given the following matrix:

m = Array[Subscript[a, #, #2] &, {4, 4}]

enter image description here

how can I find the skew diagonal or anti-diagonal or back diagonal of the matrix (shown in red)

enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ What is a "back diagonal"? Do you mean the "skew diagonal" or "anti diagonal"? $\endgroup$
    – rm -rf
    Apr 13, 2013 at 19:35
  • 1
    $\begingroup$ You can try to apply Reverse to the matrix before Diagonal. The elements will be in reverse order so you might want to apply Reverse again to the result... $\endgroup$
    – Spawn1701D
    Apr 13, 2013 at 19:38
  • $\begingroup$ Consider MapThread[Part, {Array[C, {5, 5}], -Range[5]}]. $\endgroup$ Apr 13, 2013 at 19:54

5 Answers 5

9
$\begingroup$

More 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}
$\endgroup$
10
$\begingroup$
Diagonal[Reverse /@ Array[f, {4, 4}]]

{f[1, 4], f[2, 3], f[3, 2], f[4, 1]}

$\endgroup$
1
  • 2
    $\begingroup$ ...and if you want the off-antidiagonals, just use the second argument of Diagonal[]. $\endgroup$ Apr 14, 2013 at 1:39
6
$\begingroup$

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} *)
$\endgroup$
1
  • 1
    $\begingroup$ This is two orders of magnitude faster than what I proposed. Were I to answer this question today I would use this method. A very belated +1. $\endgroup$
    – Mr.Wizard
    Aug 1, 2018 at 10:18
5
$\begingroup$

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]

enter image description here

$\endgroup$
4
  • $\begingroup$ This is probably the "correct" way to approach it, though perhaps less transparent than Reverse. $\endgroup$
    – Mr.Wizard
    Apr 13, 2013 at 23:01
  • 3
    $\begingroup$ @Mr.Wizard I was not aware that you were an advocate of "transparency" :) thanks. $\endgroup$
    – acl
    Apr 13, 2013 at 23:04
  • 2
    $\begingroup$ acl: transparent~ less~ than $\endgroup$ Apr 14, 2013 at 1:12
  • $\begingroup$ @acl Touché! (Most of my coding is quite transparent... to myself.) $\endgroup$
    – Mr.Wizard
    Apr 14, 2013 at 3:28
3
$\begingroup$

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]
$\endgroup$
1
  • 2
    $\begingroup$ If by "NE corner to the SW corner", you mean the top right to the bottom left corner, then you ... ;-) $\endgroup$ Apr 13, 2013 at 20:43

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.