replace
- arrayfire.replace(lhs: Array, rhs: Array | int | float, conditional: Array, /) None
Conditionally replaces elements of one ArrayFire array with elements from another array or a scalar value.
This function iterates over each element of the lhs array and replaces it with the corresponding element from rhs if the corresponding element in the conditional array is True. If rhs is a scalar, lhs is updated with this scalar value where the condition is True.
Parameters
- lhsArray
The left-hand side ArrayFire array whose elements may be replaced based on the condition.
- rhsArray | int | float
The right-hand side value(s) used for replacement. This can be an ArrayFire array, integer, or floating-point scalar. If rhs is an array, it must be the same size as lhs.
- conditionalArray
An ArrayFire array of boolean values indicating where replacement should occur. Must be the same size as lhs.
Examples
>>> import arrayfire as af >>> a = af.randu((3,3)) # Generate a random 3x3 array >>> a [3 3 1 1] 0.4107 0.1794 0.3775 0.8224 0.4198 0.3027 0.9518 0.0081 0.6456
>>> cond = (a >= 0.25) & (a <= 0.75) # Generate a condition array >>> cond [3 3 1 1] 1 0 1 0 1 1 0 0 1
>>> af.replace(a, 0.3333, cond) # Replace where condition is True with 0.3333 >>> a [3 3 1 1] 0.3333 0.1794 0.3333 0.8224 0.3333 0.3333 0.9518 0.0081 0.3333
Note
The lhs, rhs (if an array), and conditional arrays must be of the same size.