mindspore.ops.nonzero

mindspore.ops.nonzero(input)[source]

Return a Tensor of the positions of all non-zero values.

Parameters

input (Tensor) – nonzero input, Tensor with any dimensions. The data type is int, float or bool.

Returns

Tensor, a 2-D Tensor whose data type is int64, containing the positions of all non-zero values of the input.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.ops as ops
>>> x = Tensor(np.array([[[1,  0], [-5, 0]]]), mindspore.int32)
>>> output = ops.nonzero(x)
>>> print(output)
[[0 0 0]
 [0 1 0]]
>>> x = Tensor(np.array([1, 0, 2, 0, 3]), mindspore.int32)
>>> output = ops.nonzero(x)
>>> print(output)
[[0]
 [2]
 [4]]