MATLAB: How to replicate an array diagonally for a given number of times

blkdiagMATLABmatrixrepmat

I would like to replicate an array diagonally for a given number of times, so repmat command does not work in my case. I also tried blkdiag, but I have to write the name of the matrix 5 times if I want it to be recreated five times. I heard that it can be done with convolution from a friend of mine, yet my aim is to achieve the effect using basic commands.

Best Answer

Here is a very mundane way to do it. (I've assumed a square input, but it could easily be generalized.)
A = magic(5);
m = size(A,1);
N = 3;
AN = zeros(N*m);
for n=1:N
range = (n-1)*m+1:n*m;
AN(range,range) = A;
end