Selects elements from true_value or false_value based on pred,
analogous to R's ifelse().
Arguments
- pred
(
arrayish)
Predicate array. Must be a boolean or an R logical, and scalar or the same shape as the non-scalar arguments.- true_value, false_value
(
arrayish)
Values to return wherepredisTRUE/FALSE. Can be of any data type; the two are promoted to a common data type. Scalars (includingpred) are broadcast to the shape of the non-scalar arguments.
Value
(arrayish)
Has the common data type of true_value and false_value, and their
broadcast shape.
See also
prim_ifelse() for the underlying primitive.
Examples
pred <- nv_array(c(TRUE, FALSE, TRUE))
nv_ifelse(pred, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6)))
#> AnvlArray
#> 1
#> 5
#> 3
#> [ CPUf32{3} ]
# scalar branches are broadcast and promoted to a common data type
nv_ifelse(pred, nv_scalar(1L), nv_scalar(0.5))
#> AnvlArray
#> 1.0000
#> 0.5000
#> 1.0000
#> [ CPUf32{3} ]