Offset tile in pytorch

Hi,

I need to tile an 4-d tensor in an offset way. Showing the relevant dimensions in 2-d, if I have a tensor

[1]
[2]
[3]
[4]

(shape [4, 1]) the desired output is:

[ 1 0 0 ]
[ 2 1 0 ]
[ 3 2 1 ]
[ 4 3 2 ]

shape (4, 3).

Is there a faster way to do this than a length 3 for-loop and indexing into it?

Many thanks

1 Like

This is a type of Hankel matrix, you can do this with scipy and convert it to a torch tensor, i guess like this:

torch.tensor(scipy.linalg.hankel([4,3,2,1])[:,:-1][::-1,:])

Thanks. I need it to be pytorch based though (aside from indices) as going to backprop through it