フィルターのクリア

How to get the diagonal and a subdiagonal in the same matrix?

11 ビュー (過去 30 日間)
Jose
Jose 2014 年 10 月 15 日
コメント済み: Mohammad Abouali 2014 年 10 月 15 日
I'm trying to get matrix that only has the diagonal and the first subdiagonal values only. Say I have a 4x4 matrix
A=[ 1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16]
I want to get a matrix
B=[1 2 0 0;
0 6 7 0;
0 0 11 12;
0 0 0 16].
I know using
diag(diag(A))
will give me a 4x4 matrix with the diagonal but the part I'm hung up on is getting the subdiagonal in the form of a 4x4 matrix, I used
diag(diag(A,1))
and it gave me a 3x3 matrix of the subdiagonal. To conclude, is there a way of getting a subdiagonal in the original matrix dimensions?

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 15 日
A=[ 1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16]
B=triu(A)-triu(A,2)
B =
1 2 0 0
0 6 7 0
0 0 11 12
0 0 0 16
  2 件のコメント
Jose
Jose 2014 年 10 月 15 日
Thank you, much appreciated!
Mohammad Abouali
Mohammad Abouali 2014 年 10 月 15 日
you are welcome

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by