MATLAB: Forming a block diagonal matrix of one certain matrix

blkdiagblock diagonalmatrix

I have a matrix A which is m*n. I want to create a block diagonal matrix of size 100*100 whose diagonal elements are the matrix A.
[A,0,0,0
0,A,0,0
0,0,A,0
0,0,0,A
... ]
function, out = blkdiag(A,A,A,A,...) needs writing down the matrix so many times. Is there any other way to do this (not typing so many matrices as input arguments of blkdiag)?

Best Answer

Cell arrays create comma-separated lists, which are exactly what the blkdiag function wants as its arguments.
See if this does what you want:
A = [1 2; 3 4]; % Original Matrix (Created)
N = 3; % Number Of Times To Repeat
Ar = repmat(A, 1, N); % Repeat Matrix
Ac = mat2cell(Ar, size(A,1), repmat(size(A,2),1,N)); % Create Cell Array Of Orignal Repeated Matrix
Out = blkdiag(Ac{:}) % Desired Result
Out =
1 2 0 0 0 0
3 4 0 0 0 0
0 0 1 2 0 0
0 0 3 4 0 0
0 0 0 0 1 2
0 0 0 0 3 4