简体   繁体   English

如何找到上下对角矩阵矩阵

[英]how to find upper and lower diagonal matrix matlab

I tried to use matlab to find the upper and lower diagonal matrix in matlab 我试图使用Matlab在Matlab中找到上下对角矩阵

here is the idea 这是主意

if I have matrix 4x4 如果我有矩阵4x4

 1  2  3  4 
 5  6  7  8
 9 10 11 12 
13 14 15 16

the main diagonal is 主要对角线是

1 6 11 16

but the second upper diagonal is 但是第二对角线是

2 7 12

and the lower is 而较低的是

5 10 15

so there is triu and tril but to to write it or use any other function in matlab to find this upper and lower diagonal in matrix. 因此存在triutril但可以编写它或使用matlab中的任何其他函数在矩阵中找到该上下对角线。

just use diag , for example 例如,仅使用diag

 diag(A,0)   % main diagonal, also diag(A)
 diag(A,-1)  % lower diagonal
 diag(A,1)   % upper ...

You can use simple linear indexing to get any diagonal of a matrix. 您可以使用简单的线性索引来获取矩阵的任何对角线。 All you need to know is the linear index of the first element and the number of rows in the matrix: 您只需要知道第一个元素的线性索引和矩阵中的行数即可:

 >> [m n] = size(A);

Get the main diagonal on the matrix (first element index is 1): 获取矩阵上的主对角线(第一个元素索引为1):

 >> A( 1 : ( m+1 ) : end )

Get the lower diagonal (first index is 2): 获取较低的对角线(第一个索引为2):

 >> A( 2 : ( m+1 ) : end )

Get the upper diagonal (first index is m+1 ): 获取上对角线(第一个索引是m+1 ):

>> A( (m+1) : (m+1) : end )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM