Construct tri or penta diagonal matrix from an exisiting matrix

5 ビュー (過去 30 日間)
A
A 2015 年 2 月 25 日
コメント済み: A 2015 年 2 月 25 日
Dear all,
I am constructing a random number matrix, for instance A=randn(10,10)
For this matrix, I would like to be able to remove a tridiagonal, pentadiagonal, etc. matrix (i.e. I would like to have some simplicity in extracting this from the original matrix for any size diagonal matrix).
I know that I can get the diagonal terms that I need from the original matrix using diag(A), diag(A,-1) and diag(A,1) (for a tridiagonal), however I am not sure of a "sexy" way to populate a new matrix M with these diagonal elements after that.
Any input is greatly appreciated :)

採用された回答

John D'Errico
John D'Errico 2015 年 2 月 25 日
編集済み: John D'Errico 2015 年 2 月 25 日
Lots of ways. You need to start learning them, as that understanding of the tools in MATLAB is necessary for success.
A simple one:
tril(triu(A,-1),1)
or
diag(diag(A,-1),-1) + diag(diag(A)) + diag(diag(A,1),1)
Ok, so those are pretty simple tricks. But they represent ideas that you need to learn and understand.
If you are working with diagonal matrices, you need to learn to use tools like sparse, spy, spdiags, sub2ind, ind2sub, etc. Here, for example, I'll extract a sparse pentadiagonal part of the 10x10 matrix A.
Apenta = spdiags(spdiags(A,-2:2),-2:2,10,10);
Lots of other ways too.
  1 件のコメント
A
A 2015 年 2 月 25 日
Thank you very much, John! This gives me a lot to work from and continue learning. I appreciate it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by