Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
2  
What is a "back diagonal"? Do you mean the "skew diagonal" or "anti diagonal"? –  rm -rf Apr 13 '13 at 19:35
1  
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... –  Spawn1701D Apr 13 '13 at 19:38
    
Consider MapThread[Part, {Array[C, {5, 5}], -Range[5]}]. –  J. M. Apr 13 '13 at 19:54
add comment

5 Answers

Diagonal[Reverse /@ Array[f, {4, 4}]]

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

share|improve this answer
1  
...and if you want the off-antidiagonals, just use the second argument of Diagonal[]. –  J. M. Apr 14 '13 at 1:39
add comment

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}
share|improve this answer
add comment

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

share|improve this answer
    
This is probably the "correct" way to approach it, though perhaps less transparent than Reverse. –  Mr.Wizard Apr 13 '13 at 23:01
2  
@Mr.Wizard I was not aware that you were an advocate of "transparency" :) thanks. –  acl Apr 13 '13 at 23:04
2  
acl: transparent~ less~ than –  belisarius Apr 14 '13 at 1:12
    
@acl Touché! (Most of my coding is quite transparent... to myself.) –  Mr.Wizard Apr 14 '13 at 3:28
add comment

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]
share|improve this answer
2  
If by "NE corner to the SW corner", you mean the top right to the bottom left corner, then you ... ;-) –  Sjoerd C. de Vries Apr 13 '13 at 20:43
add comment

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} *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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