where

ivy.where(condition, x1, x2, /, *, out=None)[source]

Returns elements chosen from x or y depending on condition.

Parameters
  • condition (Union[Array, NativeArray]) – Where True, yield x1, otherwise yield x2.

  • x1 (Union[Array, NativeArray]) – values from which to choose when condition is True.

  • x2 (Union[Array, NativeArray]) – values from which to choose when condition is False.

  • 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 – An array with elements from x1 where condition is True, and elements from x2 elsewhere.

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.

Examples

With ivy.Array input:

>>> condition = ivy.array([[True, False], [True, True]])
>>> x1 = ivy.array([[1, 2], [3, 4]])
>>> x2 = ivy.array([[5, 6], [7, 8]])
>>> res = ivy.where(condition, x1, x2)
>>> print(res)
ivy.array([[1,6],[3,4]])
>>> x1 = ivy.array([[6, 13, 22, 7, 12], [7, 11, 16, 32, 9]])
>>> x2 = ivy.array([[44, 20, 8, 35, 9], [98, 23, 43, 6, 13]])
>>> res = ivy.where(((x1 % 2 == 0) & (x2 % 2 == 1)), x1, x2)
>>> print(res)
ivy.array([[ 44, 20, 8, 35, 12], [98, 23, 16, 6, 13]])

With ivy.Container input:

>>> x1 = ivy.Container(a=ivy.array([3, 1, 5]), b=ivy.array([2, 4, 6]))
>>> x2 = ivy.Container(a=ivy.array([0, 7, 2]), b=ivy.array([3, 8, 5]))
>>> res = ivy.where((x1.a > x2.a), x1, x2)
>>> print(res)
{
    a: ivy.array([3, 7, 5]),
    b: ivy.array([3, 8, 6])
}

With a mix of ivy.Array and ivy.Container inputs:

>>> x1 = ivy.array([[1.1, 2, -3.6], [5, 4, 3.1]])
>>> x2 = ivy.Container(a=ivy.array([0, 7, 2]),b=ivy.array([3, 8, 5]))
>>> res = ivy.where((x1.b < x2.b), x1, x2)
>>> print(res)
{
    a: ivy.array([0, 2, -3.6]),
    b: ivy.array([3, 4, 3.1])
}
ivy.Array.where(self, x1, x2, /, *, out=None)[source]

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

Parameters
  • self (Array) – Where True, yield x1, otherwise yield x2.

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

  • x2 (Array) – values from which to choose when condition is False.

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

Return type

Array

Returns

ret – An array with elements from self where condition is True, and elements from x2 otherwise.

Examples

>>> condition = ivy.array([[True, False], [True, True]])
>>> x1 = ivy.array([[1, 2], [3, 4]])
>>> x2 = ivy.array([[5, 6], [7, 8]])
>>> res = x1.where(condition,x2)
>>> print(res)
ivy.array([[1, 6], [3, 4]])
ivy.Container.static_where(condition, x1, x2, /, *, out=None)[source]

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

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

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

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

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

Return type

Container

Returns

ret – a container containing the values of x1 where condition is True, and x2 where condition is False.

Examples

>>> x1 = ivy.Container(a=ivy.array([3, 1, 5]), b=ivy.array([2, 4, 6]))
>>> x2 = ivy.Container(a=ivy.array([0, 7, 2]), b=ivy.array([3, 8, 5]))
>>> res = ivy.Container.static_where((x1.a > x2.a), x1, x2)
>>> print(res)
{
    a: ivy.array([3, 7, 5]),
    b: ivy.array([2, 8, 6])
}
ivy.Container.where(self, x1, x2, /, *, out=None)[source]

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

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

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

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

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

Return type

Container

Returns

ret – a container containing the values of x1 where condition is True, and x2 where condition is False.

Examples

>>> x1 = ivy.Container(a=ivy.array([3, 1, 5]), b=ivy.array([2, 4, 6]))
>>> x2 = ivy.Container(a=ivy.array([0, 7, 2]), b=ivy.array([3, 8, 5]))
>>> res = x1.where((x1.a > x2.a), x2)
>>> print(res)
{
    a: ivy.array([3, 7, 5]),
    b: ivy.array([2, 8, 6])
}

Supported Frameworks: