mindspore.numpy.where

mindspore.numpy.where(condition, x=None, y=None)[源代码]

Returns elements chosen from x or y depending on condition.

说明

As nonzero is not supported, both x and y must be provided Tensor

input.

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

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

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

返回

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

异常

ValueError – If operands cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

样例

>>> 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]]]