[Numpy-discussion] indexing to sort with argsort(..., axis=1)

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Apr 12 22:50:07 EDT 2010


On Mon, Apr 12, 2010 at 10:41 PM, Angus McMorland <amcmorl at gmail.com> wrote:
> Hi all,
> I want to sort a 2d array along one dimension, with the indices returned by
> argsort, but the subsequent indexing syntax to get the sorted array is not
> obvious.
> The following works, but I wonder if there is a simpler way:
> a = np.random.random(size=(5,3))
> s = np.argsort(a, axis=1)
> sorted = a[:,s][np.eye(5,5, dtype=bool)] # it looks like this line could be

Note: sorted is a python function

>>> b = a[:,s][np.eye(5,5, dtype=bool)]
>>> a[np.arange(a.shape[0])[:,None],s] == b
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)

Josef


> simpler
> What's the correct, concise way to do this?
> Thanks,
> Angus.
> --
> AJC McMorland
> Post-doctoral research fellow
> Neurobiology, University of Pittsburgh
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>



More information about the NumPy-Discussion mailing list