views:

16

answers:

2
+1  Q: 

Numpy sorting help

In Numpy, how do I create an array of indices which can be used return the values of the source array in sorted order? eg:

Source:
     [[4  2  6  7]
      [1  4  8  9]
      [3  1  0  3]]

Indices:
     [10  4  9  1  8  11   0  5  2  3  6  7]
+1  A: 

Take a look at numpy.argsort - it will return the indices that would sort your array. You can also specifiy the axis along which to sort. Try:

a = numpy.asarray([[4, 2, 6, 7], [1, 4, 8, 9], [3, 1, 0, 3]])
numpy.argsort(a.flat)

>> array([10,  4,  9,  1,  8, 11,  0,  5,  2,  3,  6,  7])
Jim Brissom
A: 

The answer's in the manual:

src = [[ ... ]]
ravel_src = np.ravel(src)
indices = np.argsort(ra)
HMW