How to create a block diagonal matrix without using cell array?

2 visualizaciones (últimos 30 días)
I currently have a [m x n x p] matrix. I would like to create a block diagonal consisting of [m x n] matrices for each diagonal component.
i.e.
A(:,:,1) = [1 2 3 ; 4 5 6];
A(:,:,2) = [7 8 9 ; 10 11 12];
B = [ 1 2 3 0 0 0 ;
4 5 6 0 0 0 ;
0 0 0 7 8 9;
0 0 0 10 11 12];
Is there an efficient way to do this without using a cell array ?
Thank you in advance.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 23 de Sept. de 2019
Editada: Andrei Bobrov el 23 de Sept. de 2019
A(:,:,1) = [1 2 3 ; 4 5 6];
A(:,:,2) = [7 8 9 ; 10 11 12];
C = num2cell(A,[1,2]);
B = blkdiag(C{:});
or for your case (A - matrix 3d):
[m,n,k] = size(A);
B = kron(eye(k),ones(m,n));
B(B > 0) = A;

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