2
A=[a_11, a_12; a_21, a_22]

The skew diagonal is [a_12, a_21]. Right now, I flip the matrix around and use diag.

Amro
  • 123,847
  • 25
  • 243
  • 454

1 Answers1

4

As an alternative to fliplr and diag, you can index directly into the matrix like this:

A = magic(3);
s = length(A);
idx = s:(s-1):(s*(s-1)+1);
%# for anti-diagonal, use the following 
%#idx = (s*(s-1)+1):(-s+1):s;

skewDiag = A(idx)
skewDiag =
     4     5     6
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Wouldn't this be limited to 3x3 matrices? General case? – pjama Mar 05 '11 at 03:49
  • 2
    No. This is not limited to 3x3 matrices. It is a general solution for ANY size of square matrix. Only the example was 3x3. –  Mar 05 '11 at 10:51
  • @Jonas: It seems that you are calculating the `anti diagonal` which is not what the OP requested. In some literature the `skew diagonal` is same as yours `anti diagonal`, but OP's example implies it's not the case here. Thanks – eat Mar 06 '11 at 21:47
  • @eat: I'm calculating the "main skew diagonal" (according to the Wolfram site). For a 4-by-4 array, this would be [a41,a32,a23,a14]. – Jonas Mar 06 '11 at 23:50
  • 1
    @Jonas: but isn't [4 5 6] the main skew diagonal for magic(3)? Thanks – eat Mar 07 '11 at 07:06
  • @Jonas: OK, now you made my answer superfluous ;-). Thanks – eat Mar 07 '11 at 13:18