MATLAB: How to create a meshgrid padded with a triangle of zeros in the upper-right and lower-left corners

for loopmatrixmeshgridvector

I want to take a vector, and turn it into a matrix where the original vector appears in each column, but offset down by the column number, so that I end up with an upper and lower triangle of zeros. For example, if I start with the vector
>> a=1:7
a =
1 2 3 4 5 6 7
I can turn this into a 7×4 matrix where a appears in each column by using the function meshgrid:
>> [~,b]=meshgrid(1:4,a)
b =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
But what I really want is for b to look like this:
b =
1 0 0 0
2 1 0 0
3 2 1 0
4 3 2 1
5 4 3 2
6 5 4 3
7 6 5 4
0 7 6 5
0 0 7 6
0 0 0 7
Is there an easy way to do this, without using a for loop, such as:
for h=4:-1:1
b(1:h-1,h)=zeros(h-1,1);
b(h:length(a)+h-1,h)=a;
b(length(a)+h:length(a)+3,h)=zeros(4-h,1);
end
Thanks

Best Answer

You can use
toeplitz([1:7 0 0 0],[1 0 0 0])