How to create a matrix containing the diagonal matrix of another one?

2 ビュー (過去 30 日間)
Fernando
Fernando 2014 年 3 月 25 日
コメント済み: Fernando 2014 年 3 月 26 日
Hi,
I have a diagonal block matrix and I would like to create another matrix that contains only the blocks of the diagonal (eliminating blocks out of the diagonal block). For example
A=[1 2 0 0; 3 4 0 0; 0 0 5 6; 0 0 7 8];
And I want
B=[1 2; 3 4; 5 6; 7 8];
How can I do this? Thanks,

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 25 日
編集済み: Azzi Abdelmalek 2014 年 3 月 25 日
EDIT
A=[1 2 0 0 0 0; 3 4 0 0 0 0; 0 0 5 6 0 0; 0 0 7 8 0 0;0 0 0 0 9 10; 0 0 0 0 11 12]
[n,m]=size(A);
%----------pn and pm are sub-block dimensions---------------
pn=2;
pm=2;
%-----------------------------------------------------------
ii=repmat(1:n,pm,1)
jj=reshape(1:m,pm,[])'
jj=repmat(jj,1,pn)'
idx=sub2ind(size(A),ii(:),jj(:))
A(idx)
B=reshape(A(idx),pm,[])'
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 25 日
Look at edit
Fernando
Fernando 2014 年 3 月 26 日
Great, thanks!

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

その他の回答 (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