nonzero

ivy.nonzero(x, /, *, as_tuple=True, size=None, fill_value=0)[source]

Returns the indices of the array elements which are non-zero.

Parameters
  • x (Union[Array, NativeArray]) – input array. Must have a positive rank. If x is zero-dimensional, the function must raise an exception.

  • as_tuple (bool) – if True, the output is returned as a tuple of indices, one for each (default: True) dimension of the input, containing the indices of the true elements in that dimension. If False, the coordinates are returned in a (N, ndim) array, where N is the number of true elements. Default = True.

  • size (Optional[int]) – if specified, the function will return an array of shape (size, ndim). (default: None) If the number of non-zero elements is fewer than size, the remaining elements will be filled with fill_value. Default = None.

  • fill_value (Number) – when size is specified and there are fewer than size number of elements, (default: 0) the remaining elements in the output array will be filled with fill_value. Default = 0.

Return type

Union[Tuple[Array], Array]

Returns

ret – a tuple of k arrays, one for each dimension of x and each of size n (where n is the total number of non-zero elements), containing the indices of the non-zero elements in that dimension. The indices must be returned in row-major, C-style order. The returned array must have the default array index data type.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([0, 10, 15, 20, -50, 0])
>>> y = ivy.nonzero(x)
>>> print(y)
(ivy.array([1, 2, 3, 4]),)
>>> x = ivy.array([[1, 2], [-1, -2]])
>>> y = ivy.nonzero(x)
>>> print(y)
(ivy.array([0, 0, 1, 1]), ivy.array([0, 1, 0, 1]))
>>> x = ivy.array([[0, 2], [-1, -2]])
>>> y = ivy.nonzero(x, as_tuple=False)
>>> print(y)
ivy.array([[0, 1], [1, 0], [1, 1]])
>>> x = ivy.array([0, 1])
>>> y = ivy.nonzero(x, size=2, fill_value=4)
>>> print(y)
(ivy.array([1, 4]),)

With ivy.NativeArray input:

>>> x = ivy.native_array([[10, 20], [10, 0], [0, 0]])
>>> y = ivy.nonzero(x)
>>> print(y)
(ivy.array([0, 0, 1]), ivy.array([0, 1, 0]))
>>> x = ivy.native_array([[0], [1], [1], [0], [1]])
>>> y = ivy.nonzero(x)
>>> print(y)
(ivy.array([1, 2, 4]), ivy.array([0, 0, 0]))

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0,1,2,3,0]), b=ivy.array([[1,1], [0,0]]))
>>> y = ivy.nonzero(x)
>>> print(y)
{
    a: (list[1], <class ivy.array.array.Array> shape=[3]),
    b: (list[2], <class ivy.array.array.Array> shape=[2])
}
>>> print(y.a)
(ivy.array([1, 2, 3]),)
>>> print(y.b)
(ivy.array([0, 0]), ivy.array([0, 1]))

Instance Method Examples

Using ivy.Array instance method:

>>> x = ivy.array([0,0,0,1,1,1])
>>> y = x.nonzero()
>>> print(y)
(ivy.array([3, 4, 5]),)

Using ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([1,1,1]), b=ivy.native_array([0]))
>>> y = x.nonzero()
>>> print(y)
{
    a: (list[1], <class ivy.array.array.Array> shape=[3]),
    b: (list[1], <class ivy.array.array.Array> shape=[0])
}
>>> print(y.a)
(ivy.array([0, 1, 2]),)
>>> print(y.b)
(ivy.array([]),)
ivy.Array.nonzero(self, /, *, as_tuple=True, size=None, fill_value=0)[source]

ivy.Array instance method variant of ivy.nonzero. This method simply wraps the function, and so the docstring for ivy.nonzero also applies to this method with minimal changes.

Parameters
  • self (Array) – input array. Should have a numeric data type.

  • as_tuple (bool) – if True, the output is returned as a tuple of indices, one for each (default: True) dimension of the input, containing the indices of the true elements in that dimension. If False, the coordinates are returned in a (N, ndim) array, where N is the number of true elements. Default = True.

  • size (Optional[int]) – if specified, the function will return an array of shape (size, ndim). (default: None) If the number of non-zero elements is fewer than size, the remaining elements will be filled with fill_value. Default = None.

  • fill_value (Number) – when size is specified and there are fewer than size number of elements, (default: 0) the remaining elements in the output array will be filled with fill_value. Default = 0.

Return type

Union[Tuple[Array], Array]

Returns

ret – Array containing the indices of the non-zero values.

ivy.Container.nonzero(self, /, *, as_tuple=True, size=None, fill_value=0)[source]

ivy.Container instance method variant of ivy.nonzero. This method simply wraps the function, and so the docstring for ivy.nonzero also applies to this method with minimal changes.

Parameters
  • self (Container) – input array or container. Should have a numeric data type.

  • as_tuple (bool) – if True, the output is returned as a tuple of indices, one for each (default: True) dimension of the input, containing the indices of the true elements in that dimension. If False, the coordinates are returned in a (N, ndim) array, where N is the number of true elements. Default = True.

  • size (Optional[int]) – if specified, the function will return an array of shape (size, ndim). (default: None) If the number of non-zero elements is fewer than size, the remaining elements will be filled with fill_value. Default = None.

  • fill_value (Number) – when size is specified and there are fewer than size number of elements, (default: 0) the remaining elements in the output array will be filled with fill_value. Default = 0.

Return type

Container

Returns

ret – a container containing the indices of the nonzero values.

ivy.Container.static_nonzero(x, /, *, as_tuple=True, size=None, fill_value=0)[source]

ivy.Container static method variant of ivy.nonzero. This method simply wraps the function, and so the docstring for ivy.nonzero also applies to this method with minimal changes.

Parameters
  • x (Union[Container, Array, NativeArray]) – input array or container. Should have a numeric data type.

  • as_tuple (bool) – if True, the output is returned as a tuple of indices, one for each (default: True) dimension of the input, containing the indices of the true elements in that dimension. If False, the coordinates are returned in a (N, ndim) array, where N is the number of true elements. Default = True.

  • size (Optional[int]) – if specified, the function will return an array of shape (size, ndim). (default: None) If the number of non-zero elements is fewer than size, the remaining elements will be filled with fill_value. Default = None.

  • fill_value (Number) – when size is specified and there are fewer than size number of elements, (default: 0) the remaining elements in the output array will be filled with fill_value. Default = 0.

Return type

Container

Returns

ret – a container containing the indices of the nonzero values.


Supported Frameworks: