From: Stefan v. d. W. <st...@su...> - 2006-07-11 10:20:23
|
On Tue, Jul 11, 2006 at 11:32:48AM +0200, Emanuele Olivetti wrote: > Hi, > I don't understand how to use argsort results. I have a 2D matrix and > I want to sort values in each row and obtain the index array of that > sorting. Argsort(1) is what I need, but the problem is how to use its > result in order to obtain a sorted matrix. Here is the simple example: >=20 > A =3D array([[2,3,1],[5,4,6]]) > indexes =3D a.argsort(1) >=20 > now indexes is: > array([[2, 0, 1], > [1, 0, 2]]) >=20 > I'd like to apply indexes to A and obtain: > array([[1, 2, 3], > [4, 5, 6]]) >=20 > or better, I'm interested both in a subset of indexes, i.e. indexes[:,1= :], and > the related values of A matrix. >=20 > How can I do this? If I simpy say: A[indexes] I get an IndexError. Something's not quite right here. The argsort docstring states that: argsort(a,axis=3D-1) return the indices into a of the sorted array along the given axis, so that take(a,result,axis) is the sorted array= . But N.take(A,A.argsort()) breaks. Either this is a bug, or the docstring needs to be updated. Cheers St=E9fan |