dpnp.nonzero

dpnp.nonzero(x, /)[source]

Return the indices of the elements that are non-zero.

For full documentation refer to numpy.nonzero.

Returns:

out – Indices of elements that are non-zero.

Return type:

tuple[dpnp.ndarray]

Limitations

Parameters x is supported as either dpnp.ndarray or dpctl.tensor.usm_ndarray. Otherwise the function will be executed sequentially on CPU. Input array data types are limited by supported DPNP Data types.

See also

dpnp.flatnonzero

Return indices that are non-zero in the flattened version of the input array.

dpnp.count_nonzero

Counts the number of non-zero elements in the input array.

Notes

While the nonzero values can be obtained with a[nonzero(a)], it is recommended to use x[x.astype(bool)] or x[x != 0] instead, which will correctly handle 0-d arrays.

Examples

>>> import dpnp as np
>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> out = np.nonzero(x)
>>> for arr in out:
>>>     [i for i in arr]
[0, 1, 2, 2]
[0, 1, 0, 1]
>>> x2 = np.array([3, 0, 0, 0, 4, 0, 5, 6, 0])
>>> out2 = np.nonzero(x2)
>>> for arr in out2:
>>>     [i for i in arr]
[0, 4, 6, 7]