MATLAB: Adding elements to beginning of each row

for loop

I have a 3×4 array that looks like this [1,2,3,4; 5,6,7,8; 9,10,11,12]. I want to add n number of zeros (n being the row number), so that it looks like this [0,1,2,3,4,0,0; 0,0,1,2,3,4,0; 0,0,0,1,2,3,4] Is there any way to achieve this in Matlab? rows and arrays work quite differently from my programming knowledge so I'm having a little difficulty.

Best Answer

I think the desired result could be the following.
0 1 2 3 4 0 0
0 0 5 6 7 8 0
0 0 0 9 10 11 12
Assuming this, the solution would be like this.
A = [1,2,3,4; 5,6,7,8; 9,10,11,12];
A1 = [A,zeros(3)];
B = splitapply(@(x,d) circshift(x,d), A1, (1:3)', (1:3)');