argwhere#

ivy.argwhere(x, /, *, out=None)[source]#

Return the indices of all non-zero elements of the input array.

Parameters:
  • x (Union[Array, NativeArray]) – input array, for which indices are desired.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Indices of non-zero elements.

Examples

With ivy.Array input:

>>> x = ivy.array([[1, 2], [3, 4]])
>>> res = ivy.argwhere(x)
>>> print(res)
ivy.array([[0, 0], [0, 1], [1, 0], [1, 1]])
>>> x = ivy.array([[0, 2], [3, 4]])
>>> res = ivy.argwhere(x)
>>> print(res)
ivy.array([[0, 1], [1, 0], [1, 1]])
>>> x = ivy.array([[0, 2], [3, 4]])
>>> y = ivy.zeros((3, 2), dtype=ivy.int64)
>>> res = ivy.argwhere(x, out=y)
>>> print(res)
ivy.array([[0, 1], [1, 0], [1, 1]])

With a ivy.Container input:

>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 4]))
>>> res = ivy.argwhere(x)
>>> print(res)
{
    a: ivy.array([[0], [1]]),
    b: ivy.array([[0], [1]])
}
>>> x = ivy.Container(a=ivy.array([1, 0]), b=ivy.array([3, 4]))
>>> res = ivy.argwhere(x)
>>> print(res)
{
    a: ivy.array([[0]]),
    b: ivy.array([[0], [1]])
}
Array.argwhere(self, *, out=None)[source]#

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

Parameters:
  • self (Array) – input array for which indices are desired

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Indices for where the boolean array is True.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([[1, 2], [3, 4]])
>>> res = x.argwhere()
>>> print(res)
ivy.array([[0, 0], [0, 1], [1, 0], [1, 1]])
>>> x = ivy.array([[0, 2], [3, 4]])
>>> res = x.argwhere()
>>> print(res)
ivy.array([[0, 1], [1, 0], [1, 1]])
Container.argwhere(self, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) – Boolean array, for which indices are desired.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Returns:

ret – Indices for where the boolean array is True.

Examples

Using ivy.Container instance method

>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 4]))
>>> res = x.argwhere()
>>> print(res)
{
    a: ivy.array([[0], [1]]),
    b: ivy.array([[0], [1]])
}
>>> x = ivy.Container(a=ivy.array([1, 0]), b=ivy.array([3, 4]))
>>> res = x.argwhere()
>>> print(res)
{
    a: ivy.array([[0]]),
    b: ivy.array([[0], [1]])
}