Numpy get reverse index order

Viewed 145

I have a numpy array, and I permute it with known order, how can I get the reverse order such that I can recover the input from the output?

In [1]: import numpy as np                                                      

In [2]: a = np.arange(9)                                                        

In [3]: order = np.random.permutation(9)                                        

In [4]: b = a[order]                                                            

In [5]: order                                                                   
Out[5]: array([0, 4, 5, 2, 1, 7, 6, 8, 3])

In [6]: b                                                                       
Out[6]: array([0, 4, 5, 2, 1, 7, 6, 8, 3])

In [7]: a                                                                       
Out[7]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

In [8]: inv_order = [0, 4, 3, 8, 1, 2, 6, 5, 7]                                 

In [9]: b[inv_order]                                                            
Out[9]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

Here I manually created inv_order, such that b[inv_order] is the same as input a. How can I calculate this reverse/inverse order given input, output and permuation order? Note that the input array is not guaranteed to be sorted.

Thanks!

1 Answers
Related