[Numpy-discussion] argsort

Charles R Harris charlesr.harris@gmail....
Tue Jan 15 08:44:19 CST 2013


On Tue, Jan 15, 2013 at 4:50 AM, Mads Ipsen <madsipsen@gmail.com> wrote:

>  Hi,
>
> I simply can't understand this. I'm trying to use argsort to produce
> indices that can be used to sort an array:
>
>   from numpy import *
>
>   indices = array([[4,3],[1,12],[23,7],[11,6],[8,9]])
>   args = argsort(indices, axis=0)
>   print indices[args]
>
> gives:
>
> [[[ 1 12]
>   [ 4  3]]
>
>  [[ 4  3]
>   [11  6]]
>
>  [[ 8  9]
>   [23  7]]
>
>  [[11  6]
>   [ 8  9]]
>
>  [[23  7]
>   [ 1 12]]]
>
> I thought this should produce a sorted version of the indices array.
>
> Any help is appreciated.
>
>
Fancy indexing is a funny creature and not easy to understand in more than
one dimension. What is happening is that each index is replaced by the
corresponding row of a and the result is of shape (5,2,2). To do what you
want to do:

In [20]: a[i, [[0,1]]*5]
Out[20]:
array([[ 1,  3],
       [ 4,  6],
       [ 8,  7],
       [11,  9],
       [23, 12]])

I agree that there should be an easier way to do this.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130115/c6847734/attachment.html 


More information about the NumPy-Discussion mailing list