[Numpy-discussion] efficient way to get first index of first masked/non-masked value in a masked array

Robert Kern robert.kern at gmail.com
Fri Dec 1 17:13:41 EST 2006


Matt Knox wrote:
> all I can come up with is dumb brute force methods by iterating through all the values.  Anyone got any tricks I can use?

import numpy as np

def first_masked(m):
    idx = np.where(m.mask)[0]
    if len(idx) != 0:
        return idx[0]
    else:
        raise ValueError("no masked data")


first_unmasked() is left as an exercise for the reader.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list