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

Angus McMorland amcmorl@gmail....
Tue Apr 13 07:17:39 CDT 2010


On 13 April 2010 04:01, Gökhan Sever <gokhansever@gmail.com> wrote:
>
>
> On Mon, Apr 12, 2010 at 9:41 PM, Angus McMorland <amcmorl@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 simpler
>>
>> What's the correct, concise way to do this?
>>
>
> Why not just:
>
> b = np.sort(a)
>
> What advantage does argsort provides in this case?
>

I want to be able to sort another array the same way; calculating b is
really just a check that I was doing the sort correctly.

Thanks Josef, for the reminder about using arange. I realise I've seen it
before, but haven't got it intuitive in my head yet.

A.
-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100413/7b7b7994/attachment-0001.html 


More information about the NumPy-Discussion mailing list