Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining opposite diagonal of a matrix in Matlab

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.

like image 294
Alex R. Avatar asked Dec 05 '22 09:12

Alex R.


1 Answers

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
like image 135
thefourtheye Avatar answered Jan 09 '23 00:01

thefourtheye