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

조회 수: 3 (최근 30일)
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.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 9월 23일
편집: Andrei Bobrov 2019년 9월 23일
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;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by