在Matlab中获得矩阵的对角线 [英] Obtaining opposite diagonal of a matrix in Matlab

查看:376
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆