Obtaining opposite diagonal of a matrix in Matlab

Alex R. picture Alex R. · Sep 15, 2013 · Viewed 10.2k times · Source

Let A be an matrix of size [n,n]. If I want to extract its diagonal, I do diag(A).

Actually, I want the opposite diagonal, which would be [A(n,1),A(n-1,2),A(n-2,3),...].

One way to do this is via diag(flipud(A)). However, flipud(A) is quite wasteful and multiplies the time it takes by a factor of 10 compared to finding the usual diagonal.

I'm looking for a fast way of obtaining the opposite diagonal. Naturally, for loops seem abysmally slow. Suggestions would be greatly appreciated.

Answer

thefourtheye picture thefourtheye · Sep 15, 2013

Here is my matrix, produced by A = magic(5)

A =

17    24     1     8    15
23     5     7    14    16
 4     6    13    20    22
10    12    19    21     3
11    18    25     2     9


s = size(A,1)
A(s:s-1:end-1)

ans =
11    12    13    14    15