pythonnumpymasked-array

Find where is masked in Numpy


I want to get the position in a masked array. Like this

wt[chl > 10] = 3
wt[(chl < 10) & (chl > 5)] = 2
wt[(chl < 5)] = 1
wt[(chl is masked )]=0

wt and chl is in the same shape. I want to give wt value according to the value and attribute(masked or not) of chl.

Does anyone know what I should do?

Thank you in advance.


Solution

  • argwhere returns the indices where a value is non-zero, so if the shapes are equal,

    wt[np.argwhere(chi)] = 0
    

    should zero wt where chi is nonzero (and ~np.argwhere(chi) zeroes the indices where chi is zero).