在Matlab中获得矩阵的对角线 [英] Obtaining opposite diagonal of a matrix in Matlab
问题描述
让A
为大小为[n,n]
的矩阵.如果要提取其对角线,请执行diag(A)
.
Let A
be an matrix of size [n,n]
. If I want to extract its diagonal, I do diag(A)
.
实际上,我想要相反的对角线,即[A(n,1),A(n-1,2),A(n-2,3),...]
.
Actually, I want the opposite diagonal, which would be [A(n,1),A(n-1,2),A(n-2,3),...]
.
一种方法是通过diag(flipud(A))
.但是,flipud(A)
相当浪费,并且与通常的对角线相比,花费的时间是原来的10倍.
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.
我正在寻找一种快速的方法来获取对角线.自然,for
循环看起来非常慢.建议将不胜感激.
I'm looking for a fast way of obtaining the opposite diagonal. Naturally, for
loops seem abysmally slow. Suggestions would be greatly appreciated.
推荐答案
这是我的矩阵,由A = magic(5)生成
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
这篇关于在Matlab中获得矩阵的对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!