Hi !
Say I want to use sort this list with torch: [1, 5, 3, 4]
.
The following code in Python 3.x:
from torch import tensor
import torch
t = tensor([1,5,3,4])
t = torch.sort(t, descending=True)
print(t)
returns
torch.return_types.sort(
values=tensor([5, 4, 3, 1]),
indices=tensor([1, 3, 2, 0]))
The list indices
from the output is a permutation of 0, 1, 2, 3. It maps the output values [5, 4, 3, 1]
to the input list [1, 5, 3, 4]
. Indeed : 5 goes to position 1, 4 to position 3, 3 to position 2 and 1 to position 0 : [1, 3, 2, 0]
.
What would be a simple and efficient way to get the inverse of this permutation, that is the permutation that maps the input list [1, 5, 3, 4]
to the output list [5, 4, 3, 1]
? This inverse is in our case: [3, 0, 2, 1]
.
Thanks a lot !