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.
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