[Numpy-discussion] Find indices of largest elements
Gökhan Sever
gokhansever@gmail....
Wed Apr 14 10:56:56 CDT 2010
On Wed, Apr 14, 2010 at 10:16 AM, Nikolaus Rath <Nikolaus@rath.org> wrote:
> Hello,
>
> How do I best find out the indices of the largest x elements in an
> array?
>
> Example:
>
> a = [ [1,8,2], [2,1,3] ]
> magic_function(a, 2) == [ (0,1), (1,2) ]
>
> Since the largest 2 elements are at positions (0,1) and (1,2).
>
> I[1]: a = np.array([ [1,8,2], [2,1,3] ])
I[2]: b = a.max(axis=1)[:,np.newaxis]
I[3]: b
O[3]:
array([[8],
[3]])
I[4]: np.where(a==b)
O[4]: (array([0, 1]), array([1, 2]))
--
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100414/b0c17081/attachment.html
More information about the NumPy-Discussion
mailing list