MATLAB: Diagonal displacement of matrix into another matrix

matrix

Hi,
I have this matrix 2X8:
and from this I need to create 4 matrices of zeros 5X5 where I displace the 2X8 matrix thi way:
Note that I have to do this parametrically so that if for exaple I have a matrix 2X10 it needs to be dsiplaced in 5 matrices 6X6 and so on.
Any suggestions?

Best Answer

Call your matrix A,
A=reshape(1:16,2,[]), %example input
A = 2×8
1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16
N=size(A,2)/2;
outputMatrices=cell(1,N);
z0=num2cell(zeros(1,N));
for i=1:N
z=z0; z{i}=A(:,2*i-1:2*i);
outputMatrices{i}=blkdiag(z{:});
end
outputMatrices{:} %the resulting matrices
ans = 5×5
1 3 0 0 0 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 5 7 0 0 0 6 8 0 0 0 0 0 0 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 9 11 0 0 0 10 12 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 15 0 0 0 14 16