-
numpy.ma.where(condition, x=, y=)
[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
whencondition
is True, and fromy
otherwise. If neitherx
nory
are given, the function returns a tuple of indices wherecondition
is True (the result ofcondition.nonzero()
).Parameters: condition : array_like, bool
The condition to meet. For each True element, yield the corresponding element from
x
, otherwise fromy
.x, y : array_like, optional
Values from which to choose.
x
andy
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
andy
were given, otherwise the result ofcondition.nonzero()
.See also
-
numpy.where
- Equivalent function in the top-level NumPy module.
Examples
123456789>>> 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
]))
1234>>>
print
(np.ma.where(x >
5
, x,
-
3.1416
))
[[
-
3.1416
-
-
-
3.1416
]
[
-
-
-
3.1416
-
-
]
[
6.0
-
-
8.0
]]
-
numpy.ma.where()

2025-01-10 15:47:30
Please login to continue.