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

2 Ansichten (letzte 30 Tage)
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,

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 25 Mär. 2014
Bearbeitet: Azzi Abdelmalek am 25 Mär. 2014
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by