Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector projection on matrix along diagonal

Tags:

matrix

matlab

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

like image 655
liborw Avatar asked Apr 07 '10 17:04

liborw


1 Answers

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.

like image 116
gnovice Avatar answered Oct 13 '22 08:10

gnovice