# How to quickly inverse a permutation by using PyTorch?

**URL:** <https://discuss.pytorch.org/t/how-to-quickly-inverse-a-permutation-by-using-pytorch/116205>\
**Category:** Uncategorized\
**Created:** [March 28, 2021, 2:49am UTC](https://discuss.pytorch.org/t/how-to-quickly-inverse-a-permutation-by-using-pytorch/116205 "2021-03-28T02:49:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [March 28, 2021, 3:52am UTC](https://discuss.pytorch.org/t/how-to-quickly-inverse-a-permutation-by-using-pytorch/116205/2 "2021-03-28T03:52:40Z")

</div>

Hi Bin!

> [@111414](#):
>
> ```auto
> Example #1:
> [x, y, z] shuffled by P: [2, 0, 1], we will obtain [z, x, y],
> the corresponding inverse should be P^-1: [1, 2, 0].
> 
> Example #2:
> [a, b, c, d, e, f] shuffled by P: [5, 2, 0, 1, 4, 3], then we will get [f, c, a, b, e, d],
> the corresponding inverse should be P^-1: [2, 3, 1, 5, 4, 0].
> 
> ```

If you represent your permutation as a vector of permuted integers  
(as you do), you may use `argsort()` to obtain the inverse permutation:

```plaintext
>>> import torch
>>> torch. __version__'1.7.1'
>>> p1 = torch.tensor ([2, 0, 1])
>>> torch.argsort (p1)
tensor([1, 2, 0])
>>> p2 = torch.tensor ([5, 2, 0, 1, 4, 3])
>>> torch.argsort (p2)
tensor([2, 3, 1, 5, 4, 0])

```

Best.

K. Frank

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-quickly-inverse-a-permutation-by-using-pytorch/116205)._
