select

arrayfire.select(lhs: Array | int | float, rhs: Array | int | float, conditional: Array, /) Array

Conditionally selects elements from one of two sources (ArrayFire arrays or scalars) based on a condition array.

This function iterates over each element of the conditional array. For elements where conditional is True, it selects the corresponding element from lhs; otherwise, it selects from rhs. The lhs and rhs can be either ArrayFire arrays or scalar values, but at least one of them must be an ArrayFire array.

Parameters

lhsArray | int | float

The left-hand side source for selection. Can be an ArrayFire array or a scalar value. Elements from lhs are selected where conditional is True.

rhsArray | int | float

The right-hand side source for selection. Can be an ArrayFire array or a scalar value. Elements from rhs are selected where conditional is False.

conditionalArray

An ArrayFire array of boolean values that serve as the condition for selection.

Raises

TypeError

If neither lhs nor rhs is an ArrayFire array.

Examples

>>> import arrayfire as af
>>> a = af.randu((3,3))  # Generate a random 3x3 array
>>> b = af.randu((3,3))  # Generate another random 3x3 array
>>> cond = a > b  # Generate a boolean condition array
>>> a
[3 3 1 1]
    0.4107     0.1794     0.3775
    0.8224     0.4198     0.3027
    0.9518     0.0081     0.6456
>>> b
[3 3 1 1]
    0.7269     0.3569     0.3341
    0.7104     0.1437     0.0899
    0.5201     0.4563     0.5363
>>> af.select(cond, a, b)  # Conditionally select between `a` and `b`
[3 3 1 1]
    0.7269     0.3569     0.3775
    0.8224     0.4198     0.3027
    0.9518     0.4563     0.6456

Note

  • The conditional array must be of the same size as both lhs and rhs if they are arrays.

  • At least one of lhs or rhs must be an ArrayFire array.