def putmask(array, mask, values): """putmask() sets elements of 'array' for which 'mask' is non-zero to the corresponding element in 'values'. 'array' must be an array. """ bmask = _nc.asarray(mask) bvalues = _nc.asarray(values) if bmask.nelements() == array.nelements(): if bmask._shape != array._shape: bmask = bmask.view() bmask.setshape(array.getshape()) else: bmask = _broadcast_or_resize(array, bmask) bvalues = _broadcast_or_resize(array, bvalues) choose(bmask != 0, (array, bvalues), array)
def where(condition, x=None, y=None, out=None): """where() returns an array shaped like 'condition' with elements selected from 'x' or 'y' by the 1 or 0 value of each condition element, respectively. If neither 'x' nor 'y' is specified, where acts as a synonym for nonzero(). """ if x is None and y is None: if out is None: return ufunc.nonzero(condition) else: raise ValueError("single parameter where() does not support output array") else: if x is None or y is None: raise ValueError("Invalid parameter list") return choose(ufunc.not_equal(condition, 0), (y,x), out)
def clip(m, m_min, m_max): """clip() returns a new array with every entry in m that is less than m_min replaced by m_min, and every entry greater than m_max replaced by m_max. """ selector = ufunc.less(m, m_min)+2*ufunc.greater(m, m_max) return choose(selector, (m, m_min, m_max))