Is there a way to insert a tensor into an existing tensor?

Suppose there is a tensor of torch.FloatTensor x with size 3x4:
1.2045 2.4084 0.4001 1.1372
0.5596 1.5677 0.6219 -0.7954
1.3635 -1.2313 -0.5414 -1.8478

Is it possible to insert a tensor(e.g.,[ 0 0 0 0]) with size 1x4 into the second line?

The result should be:
1.2045 2.4084 0.4001 1.1372
0.0000 0.0000 0.0000 0.0000
0.5596 1.5677 0.6219 -0.7954
1.3635 -1.2313 -0.5414 -1.8478

1 Like

The following code shows you how to do it:

a = torch.rand(3, 4)
b = torch.zeros(1, 4)

idx = 1
c = torch.cat([a[:idx], b, a[idx:]], 0)
7 Likes