Home > Back-end >  Map tensor values to another tensor
Map tensor values to another tensor

Time:10-02

Hi everyone I have two tensors, one tensor has as values the indicies to another tensor which contain the values I want mapped for example:

a = tensor([0, 2, 5, 1, 0, 0, 4, 3, 0, 0, 0, 2,2,1])
b = tensor([1.5, 2.7, 1.8, 7.0, 3.9, 10.0])

I would like to produce a new tensor in which the values of b replace the index values in a as such:

new_tensor = tensor([1.5, 1.8, 10.0, 2.7, 1.5, 1.5, ... ])

Id like to do it without performing some time of for loop. If there is a pytorch/linear away to tackle the mapping that'd be great.

CodePudding user response:

>>> b[a]
tensor([ 1.5000,  1.8000, 10.0000,  2.7000,  1.5000,  1.5000,  3.9000,  7.0000,
         1.5000,  1.5000,  1.5000,  1.8000,  1.8000,  2.7000])
  • Related