MATLAB: How to create a block diagonal matrix without using cell array

3d matrixblkdiagblock diagonal

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.

Best Answer

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;