0

Edited since my last question was a duplicate, but I'm struggling with this as well. I'm currently working with a matrix and can easily find the largest element with

M[M != 1].max()

However, I'm interested in getting the N largest elements and can't find an easy way to do this with matrices. Is there an efficient solution?

planner15
  • 53
  • 8
  • 4
    Hi @planner15 - as per the link, `np.where(M == M[M != 0].min())` should return the row and column indices corresponding to the minimum element. – Alex Riley Apr 17 '15 at 16:20
  • The revised question is still a duplicate, see [this question](http://stackoverflow.com/q/10337533/2379410), and [this question](http://stackoverflow.com/q/6910641/2379410) for finding the indices. –  Apr 17 '15 at 17:59
  • @moarningsun I saw those, but those are for arrays. Would they still apply to matrices? – planner15 Apr 17 '15 at 18:22
  • @planner15: The same basic idea applies, but you may need to do some small syntactical changes. –  Apr 17 '15 at 19:04

1 Answers1

0

Yes there is a where method which takes a condition as one of the parameter ,

minimum = M[M != 0].min()

print numpy.where(M==minimum)
ZdaR
  • 22,343
  • 7
  • 66
  • 87