HMW
HMW

Reputation: 13

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]

Upvotes: 1

Views: 161

Answers (2)

HMW
HMW

Reputation: 13

The answer's in the manual:

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

Upvotes: 0

Jim Brissom
Jim Brissom

Reputation: 32949

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])

Upvotes: 2

Related Questions