MATLAB: Create a matrix of this type

for loopmatrix arraymatrix manipulation

Hello,
I want to make a matrix this type
1 0 0 0 0 0
2 0 0 0 0 0
3 4 0 0 0 0
5 6 0 0 0 0
7 8 9 0 0 0
10 11 12 0 0 0
13 14 15 16 0 0
17 18 19 20 0 0
% Alternate rows have same number of element
%Each element of the matrix is 1 larger than previous one
How to achieve it ?

Best Answer

m = 8;
n = 6;
P = kron(triu(ones(n,m/2)),[1,1]);
P(P>0) = 1:nnz(P);
out = P';