argwhere

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

Returns 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]) – optional output array, for writing the result to. It must have a shape that the (default: None) 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]])
}
ivy.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]) – optional output array, for writing the result to. It must have a shape (default: None) 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]])
ivy.Container.static_argwhere(x, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]

ivy.Container static 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 – Boolean array, for which indices are desired.

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

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

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

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

Return type

Container

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 = ivy.Container.static_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.Container.static_argwhere(x)
>>> print(res)
{
    a: ivy.array([[0]]),
    b: ivy.array([[0], [1]])
}
ivy.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]]]) – The key-chains to apply or not apply the method to. Default is None. (default: None)

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

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

  • map_sequences (bool) – Whether to also map method to sequences (lists, tuples). (default: False) 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]])
}

Supported Frameworks: