Is there a easy way how to produce following matrix:
a =
4 5 6 7
3 4 5 6
2 3 4 5
1 2 3 4
which is a projection of vector [1 2 3 4 5 6 7] along diagonal?
thanks
You can do this using the functions HANKEL and FLIPUD:
a = flipud(hankel(1:4,4:7));
Or using the functions TOEPLITZ and FLIPLR:
a = toeplitz(fliplr(1:4),4:7);
a = toeplitz(4:-1:1,4:7); %# Without fliplr
You could also generalize these solutions to an arbitrary vector where you have chosen the center point at which to break the vector. For example:
>> vec = [6 3 45 1 1 2]; %# A sample vector
>> centerIndex = 3;
>> a = flipud(hankel(vec(1:centerIndex),vec(centerIndex:end)))
a =
45 1 1 2
3 45 1 1
6 3 45 1
The above example places the first three elements of the vector running up the first column and the last four elements of the vector running along the first row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With