SciPy

numpy.ma.where

numpy.ma.where(condition, x=None, y=None)[source]

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

Returns a masked array, shaped like condition, where the elements are from x when condition is True, and from y otherwise. If neither x nor y are given, the function returns a tuple of indices where condition is True (the result of condition.nonzero()).

Parameters:

condition : array_like, bool

The condition to meet. For each True element, yield the corresponding element from x, otherwise from y.

x, y : array_like, optional

Values from which to choose. x and y need to have the same shape as condition, or be broadcast-able to that shape.

Returns:

out : MaskedArray or tuple of ndarrays

The resulting masked array if x and y were given, otherwise the result of condition.nonzero().

See also

numpy.where
Equivalent function in the top-level NumPy module.

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]]
>>> np.ma.where(x > 5)    # return the indices where x > 5
(array([2, 2]), array([0, 2]))
>>> print np.ma.where(x > 5, x, -3.1416)
[[-3.1416 -- -3.1416]
 [-- -3.1416 --]
 [6.0 -- 8.0]]

Previous topic

numpy.ma.indices

Next topic

Mathematical functions