larray.where

larray.where(condition, x, y)

Return elements, either from x or y, depending on condition.

Parameters
conditionboolean Array

When True, yield x, otherwise yield y.

x, yArray

Values from which to choose.

Returns
outArray

If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.

Examples

>>> from larray import Array
>>> arr = Array([[10, 7, 5, 9],
...               [5, 8, 3, 7],
...               [6, 2, 0, 9],
...               [9, 10, 5, 6]], "a=a0..a3;b=b0..b3")
>>> arr
a\b  b0  b1  b2  b3
 a0  10   7   5   9
 a1   5   8   3   7
 a2   6   2   0   9
 a3   9  10   5   6

Simple use

>>> where(arr <= 5, 0, arr)
a\b  b0  b1  b2  b3
 a0  10   7   0   9
 a1   0   8   0   7
 a2   6   0   0   9
 a3   9  10   0   6

With broadcasting

>>> mean_by_col = arr.mean('a')
>>> mean_by_col
b   b0    b1    b2    b3
   7.5  6.75  3.25  7.75
>>> # for each column, set values below the mean value to the mean value
>>> where(arr < mean_by_col, mean_by_col, arr)
a\b    b0    b1    b2    b3
 a0  10.0   7.0   5.0   9.0
 a1   7.5   8.0  3.25  7.75
 a2   7.5  6.75  3.25   9.0
 a3   9.0  10.0   5.0  7.75