mindspore.numpy.where
- mindspore.numpy.where(condition, x=None, y=None)[源代码]
- 根据 - condition从- x或- y中选择元素。- 说明 - 由于不支持 - nonzero,- x和- y必须都是Tensor输入。- 参数:
- condition (Tensor) - 当为 - True时,选择- x中的值,否则选择- y中的值。
- x (Tensor,可选) - 选择值的来源。默认值: - None。
- y (Tensor,可选) - 选择值的来源。 - x,- y和- condition需要能够广播到相同的shape。默认值:- None。
 
- 返回:
- Tensor或标量,其中 - condition为- True的位置取自- x,其他位置取自- y。
- 异常:
- ValueError - 如果操作数不能广播到相同的shape。 
 
- 支持平台:
- 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]]]