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

2 vues (au cours des 30 derniers jours)
Fernando
Fernando le 25 Mar 2014
Commenté : Fernando le 26 Mar 2014
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,

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 25 Mar 2014
Modifié(e) : Azzi Abdelmalek le 25 Mar 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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 25 Mar 2014
Look at edit
Fernando
Fernando le 26 Mar 2014
Great, thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by