A = [5,7,6] and I = [2,0,1].
I want to reorganize A such that I get A_new = [7,6,5]
However, when I use A_new = numpy.take_along_axis(A,I,1), I get A_new = [6,5,7].
In my case, index of 5 should be 2 instead of index of 6 being 2. Please let me know how this can be done without using for loops.
However, when I use A_new = numpy.take_along_axis(A,I,1), I get A_new = [6,5,7].
The 'trick' is to use
Ito assign values to a 'black' array:Using
Idirectly as index ofAdoesn't do what you want (but often does for a different definition ofI). Comments have suggestedargsort:argsortin effect convertsIto one that does indexAcorrectly.In your definition of
I, you wantA[0]to be in theres[2]slot of the result. It's the difference between saying I want to move the 0'th item to the 2'th slot, versus I want 0's result to be 1'st' item.