How do I map the tensor

Screenshot from 2022-02-18 11-51-22

the left two tensors are the source value and index of each value at target tensor. I wanted to map the source value to the target tensor based on its index. e.g. a1’s index is 3 so a1 went to the third position target sensor. How can I do this in pytorch? Any help will be apprecaited!

It sounds like this is basically implemented by scatter_add_ depending on how exactly you want the 1-based indexing/ignoring values mapped to 0 to work.

>>> import torch
>>> a = torch.arange(10).float() + 1
>>> a
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
>>> index = torch.tensor([3, 3, 0, 0, 0, 0, 0, 0, 0, 1])
>>> target = torch.zeros(11)
>>> target.scatter_add_(0, index, a)[1:]
tensor([10.,  0.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])