[Numpy-discussion] Find indices of largest elements
Wed Apr 14 10:48:26 CDT 2010
On Wed, Apr 14, 2010 at 11:16 AM, Nikolaus Rath <Nikolaus@rath.org> wrote:
> How do I best find out the indices of the largest x elements in an
> 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).
Something like this might be made to work, if you want the max
elements along all the rows.
In : a = np.asarray(a)
In : a[range(len(a)),np.argmax(a, axis=1)]
Out: array([8, 3])
More information about the NumPy-Discussion