[NumPy-Tickets] [NumPy] #1673: first nonzero element

NumPy Trac numpy-tickets@scipy....
Sat Jul 9 07:48:03 CDT 2011

#1673: first nonzero element
 Reporter:  tom3118      |       Owner:  somebody
     Type:  enhancement  |      Status:  new     
 Priority:  normal       |   Milestone:  2.0.0   
Component:  Other        |     Version:  1.5.0   
 Keywords:               |  
Changes (by lcampagn):

 * cc: lcampagn@… (added)


 I have seen many requests for a find_first in numpy, but most of these
 requests have subtly different (and incompatible) requirements such as
 "find first value less than x" or "find first nonzero value". I suggest
 the following function specification:
   ind = array.find(x, testOp='eq', arrayOp='all', axis=0, test=None)
     x       -> value to search for
     testOp  -> condition to test for ('eq', 'ne', 'gt', 'lt', 'ge', 'le')
     arrayOp -> method for joining multiple comparisons ('any' or 'all')
     axis    -> the axis over which to search
     test    -> for convenience, this may specify a function to call to
                the test. This is not expected to be efficient.
     first index where condition is true (or test returns true, if given)
     or None if the condition was never met

 If the array has ndim > 1, then tests are performed using normal
 broadcasting rules.
 So for example, if I have an array with shape (2,3), the following would
 be valid:

   ## find first row with all values=0
   array.find(0, testOp='eq', arrayOp='all', axis=0)
   ## equivalent to:
   for i in range(array.shape[axis]):
     if (array[i] == 0).all():
       return i

   ## find first column with any element greater than its corresponding
 element in col
   col = array([1,2])
   array.find(col, testOp='gt', arrayOp='any', axis=1)
   ## equivalent to:
   for i in range(array.shape[axis]):
     if (array[:,i] == col.any():
       return i

Ticket URL: <http://projects.scipy.org/numpy/ticket/1673#comment:4>
NumPy <http://projects.scipy.org/numpy>
My example project

More information about the NumPy-Tickets mailing list