Add several columns to a matrix without any loop

Hi,

Suppose we have a tensor with dimension of [2, 1, 2, 10] and we want to add 3 columns between columns 3,4 and columns 5,6 and columns 8,9.
Is it possible to add these columns to the tensor without any loop?
If not, according to https://discuss.pytorch.org/t/how-can-i-insert-a-row-into-a-floattensor/18049/2 it needs 2 cat operations for adding each new column; is there any faster methods for doing it?

Thanks

You can use indexed assignments:

a = torch.ones(2, 1, 2, 10)
b =  = torch.full((2, 1, 2, 3), 2.0)

xa = torch.empty(2, 1, 2, 13)
xa[:,:,:,[0, 1, 2, 3, 4+1, 5+1, 6+2, 7+2, 8+3, 9+3]] = a
xa[:,:,:,[4, 7, 10]] = b
print(xa)

If your columns are a little larger (say 100 instead of 4 elements), a for loop might also not be too bad.

Best regards

Thomas

Thanks a lot. It could help me.
Best Regards