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

1 visualización (últimos 30 días)
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,

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 25 de Mzo. de 2014
Editada: Azzi Abdelmalek el 25 de Mzo. de 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 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by