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

2 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by