简体   繁体   中英

How do I reverse argsort to point to the original unsorted array?

I'm annoyed with myself for not figuring this out on my own; it is most likely trivial. But anyway, suppose I have an unsorted array, and an argsort for sorting it:

a = array([83, 75, 60, 80, 20,  6,  37, 81,  7, 21])
p = a.argsort()
b = a[p]

So the array b is the sorted version of array a . Now I have a function which chooses certain values from a sorted list; suppose that function returns the list

f = [0, 1, 2, 3, 6, 7]

These are indices for the sorted array b . But how do I "invert" the sort so that I can obtain the indices which point to the corresponding values of a ? In this case, we have

b[f]
[ 6  7 20 21 75 80]

and the corresponding indices for a are

af = [5, 8, 4, 9, 1, 3]

How can I most easily determine af from f , a and p ?

You don't need to invert anything. Remember that p is the index into a corresponding to each element of b . The f th element of b comes from a indexed with the f th element of p :

a[p[f]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM