numpy.putmask

numpy.putmask(a, mask, values)

Changes elements of an array based on conditional and input values.

Sets a.flat[n] = values[n] for each n where mask.flat[n] is true.

If values is not the same size as a and mask then it will repeat. This gives behavior different from a[mask] = values.

Parameters:

a : array_like

Array to put data into

mask : array_like

Boolean mask array

values : array_like

Values to put

See also

place, put, take

Examples

>>> a = np.array([10,20,30,40])
>>> mask = np.array([True,False,True,True])
>>> a.putmask([60,70,80,90], mask)
>>> a
array([60, 20, 80, 90])
>>> a = np.array([10,20,30,40])
>>> a[mask]
array([60, 80, 90])
>>> a[mask] = np.array([60,70,80,90])
>>> a
array([60, 20, 70, 80])
>>> a.putmask([10,90], mask)
>>> a
array([10, 20, 10, 90])
>>> np.putmask(a, mask, [60,70,80,90])

Previous topic

numpy.put

Next topic

numpy.ndenumerate

This Page

Quick search