Mapping one tensor of values and another tensor of indices to a third tensor by index

I have one_tensor (3,1) of values, say:

one_tensor = torch.tensor([[24],[36],[12]])

and index_tensor (3,1) of indices, say:

index_tensor = torch.tensor([[0],[3],[2]])

I’d like to map the values of one_tensor to a third_tensor (3,4) of zeros:

third_tensor = torch.zeros(3,4)

using the indices of index_tensor so that third tensor ends up like this:

third_tensor = tensor([[24, 0, 0, 0], [0, 0, 0, 36], [0, 0, 12, 0]])

Is there a fast, efficient way to do this without manually iterating over each row?

.scatter_ should work:

third_tensor.scatter_(1, index_tensor, one_tensor)
print(third_tensor)
> tensor([[24.,  0.,  0.,  0.],
        [ 0.,  0.,  0., 36.],
        [ 0.,  0., 12.,  0.]])