[Numpy-discussion] retrieving original array locations from 2d argsort

Moroney, Catherine M (398D) Catherine.M.Moroney at jpl.nasa.gov
Mon Jul 15 21:03:26 EDT 2013

I know that there's an easy way to solve this problem, but I'm not sufficiently knowledgeable
about numpy indexing to figure it out.

Here is the problem:

Take a 2-d array a, of any size.
Sort it in ascending order using, I presume, argsort.
Step through the sorted array in order, and for each element in the sorted array,
retrieve what the corresponding (line, sample) indices in the original array are.

For instance:

a = numpy.arange(0, 16).reshape(4,4)
a[0,:] = -1*numpy.arange(0,4)
a[2,:] = -1*numpy.arange(4,8)

asort = numpy.sort(a, axis=None)
for idx in xrange(0, asort.size):
	element = asort[idx]
        !! Find the line and sample location in a that corresponds to the i-th element in assort

Thank-you for your help,


More information about the NumPy-Discussion mailing list