How to create a block diagonal matrix without using cell array?
2 views (last 30 days)
Show older comments
Thanacha Choopojcharoen
on 23 Sep 2019
Commented: Thanacha Choopojcharoen
on 23 Sep 2019
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.
0 Comments
Accepted Answer
Andrei Bobrov
on 23 Sep 2019
Edited: Andrei Bobrov
on 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;
More Answers (0)
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!