Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 23 Sep. 2019
Bearbeitet: Andrei Bobrov am 23 Sep. 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;

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