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?
CodePudding user response:
You don't need to invert anything. Remember that p is the index into a corresponding to each element of b. The fth element of b comes from a indexed with the fth element of p:
a[p[f]]
In other words,
af = p[f]
