numpy.ma.where

numpy.ma.where(condition, x=<no value>, y=<no value>)[源代码]

Return a masked array with elements from x or y, depending on condition.

注解

When only condition is provided, this function is identical to nonzero. The rest of this documentation covers only the case where all three arguments are provided.

Parameters:
condition : array_like, bool

Where True, yield x, otherwise yield y.

x, y : array_like, optional

Values from which to choose. x, y and condition need to be broadcastable to some shape.

Returns:
out : MaskedArray

An masked array with masked elements where the condition is masked, elements from x where condition is True, and elements from y elsewhere.

参见

numpy.where
Equivalent function in the top-level NumPy module.
nonzero
The function that is called when x and y are omitted

Examples

>>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0],
...                                                    [1, 0, 1],
...                                                    [0, 1, 0]])
>>> print(x)
[[0.0 -- 2.0]
 [-- 4.0 --]
 [6.0 -- 8.0]]
>>> print(np.ma.where(x > 5, x, -3.1416))
[[-3.1416 -- -3.1416]
 [-- -3.1416 --]
 [6.0 -- 8.0]]