I have a value tensor and a raw index, how can I get a 2-D tensor

the value tensor is value = torch.Tensor([2, 9, 7, 8, 4])
the raw index tensor = torch.Tensor([0, 0, 1, 2, 3])

2 matches 0 which means 2 in 0th raw, and because the desired[0][0]=2, the 9 matches 0 but the desired[0][0]==2, so desired[0][1]=9

I do want get the 2-D tensor
desired tensor:

tensor([[2., 9.],

[7., 0.],

[8., 0.],

[4., 0.]])

I don’t fully understand the example, as it seems you would like to “fill” double indices into the second column. However, your index tensor doesn’t contain a 4, so how is the last row created in the output?
Also, what is expected, if e.g. 5 zeros are used in the index tensor? Would the result contain all source values in row0 and what would the output shape be?

EDIT: Also double post from here.

If I understand properly, this could help your

>>> import torch
>>> value = torch.tensor([2,9,7,8,4])
>>> row_of_value = torch.tensor([0,0,1,2,3])
>>> vec_row = [torch.zeros((row_of_value==i).sum())+value[row_of_value==i] for i in range(torch.max(row_of_value)+1)]
>>> vec_row
[tensor([2., 9.]), tensor([7.]), tensor([8.]), tensor([4.])]
>>> torch.nn.utils.rnn.pad_sequence(vec_row,batch_first=True)
tensor([[2., 9.],
        [7., 0.],
        [8., 0.],
        [4., 0.]])