mindspore.numpy.where

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

Returns elements chosen from x or y depending on condition.

Note

As nonzero is not supported, neither x or y can be None.

Parameters
  • condition (Tensor) – where True, yield x, otherwise yield y.

  • x (Tensor) – Values from which to choose.

  • y (Tensor) – Values from which to choose. x, y and condition need to be broadcastable to some shape.

Returns

Tensor or scalar, with elements from x where condition is True, and elements from y elsewhere.

Raises

ValueError – if operands cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> condition = np.full((1, 1, 2), [False, True])
>>> x = np.full((1, 3, 2), 5)
>>> y = np.full((2, 1, 1), 7)
>>> output = np.where(condition, x, y)
>>> print(output)
[[[7 5]
[7 5]
[7 5]]
[[7 5]
[7 5]
[7 5]]]