where

Contents

where#

class brainunit.math.where(condition, x=None, y=None, /, *, size=None, fill_value=None, **kwargs)#

Return elements chosen from x or y depending on condition.

Note

When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the case where all three arguments are provided.

Parameters:
  • condition (array_like, bool,) – Where True, yield x, otherwise yield y.

  • x (array_like, Quantity) – Values from which to choose. x, y and condition need to be broadcastable to some shape.

  • y (array_like, Quantity) – Values from which to choose. x, y and condition need to be broadcastable to some shape.

  • size (int, optional) – The length of the output array. If size is not None, the output array will have the length of size.

  • fill_value (scalar, Quantity, optional) – The value to use for missing values. If fill_value is not None, the output array will have the length of size.

Returns:

out – An array with elements from x where condition is True, and elements from y elsewhere.

Return type:

ndarray

See also

choose

nonzero

The function that is called when x and y are omitted

Examples

>>> import saiunit as u
>>> a = [1, 2, 3, 4, 5] * u.meter
>>> u.math.where(a > 3 * u.meter, a, 0 * u.meter)