Copying indexed slices of a tensor to an empty tensor

Hi guys, I’m new here and I have a problem. I have a tensor with data mat and an empty tensor of the same shape. I also have a 1D index tensor ‘ind’, which has indices of slices I want to copy. What I want to do is to look at index of a slice I want and put it in the same spot in the empy tensor.

mat = torch.arange(0,16).reshape(4,2,2)
empty = torch.zeros((4,2,2))
ind = torch.tensor([0,2])

The idea is to do something like this ( which I know is wrong, I’m new to this :frowning: ):

empty[ind] = mat[ind]

so here the empty matrix should have first and third slices, while second and fourth slices should
remain empty ( or in this case filled with zeros ):

torch.tensor( [ [ [ 0, 1 ],
                         [ 2, 3 ] ],
                       [ [ 0 , 0 ],
                         [ 0 , 0 ] ],
                        [ [ 8, 9 ],
                         [10, 11 ] ],
                         [ [ 0 , 0 ],
                         [ 0 , 0 ] ] ] )

How do I solve this?
Thank you for your answers.

Your code seems to be working fine, if you get rid of the type mismatch error by calling float() on mat :slight_smile:

mat = torch.arange(0,16).reshape(4,2,2).float()
empty = torch.zeros((4,2,2))
ind = torch.tensor([0,2])
empty[ind] = mat[ind]
print(empty)
> tensor([[[ 0.,  1.],
           [ 2.,  3.]],

          [[ 0.,  0.],
           [ 0.,  0.]],

          [[ 8.,  9.],
           [10., 11.]],

          [[ 0.,  0.],
           [ 0.,  0.]]])

Thank you so much! I think I was being too vague, with calling tensor as an index. Now the code is working properly :slight_smile: