As a simple example, I've got the following:
import numpy as np
a = np.matrix([[0.34, 0.44, 0.21, 0.51]])
a_max = np.matrix([[0.35, 0.40, 0.20, 0.50]])
I would like to apply a transformation where anything in a, that is greater than a_max, is capped at a_max. I have tried to do this via:
a[a>a_max] = a_max[a>a_max]
however this throws an error:
ValueError: array is not broadcastable to correct shape
What is the proper way to do this? Ignore the fact that I am doing a simple maximum (I am guessing bumpy may have builtin tools to do that particular problem). My real problem uses a much more complicated set of booleans to create a boolean mask, which then should replace values from a replacement matrix.
Life's much easier if you work with arrays and not matrices; it Just Works (tm).
I guess you could use
np.where
, though:The
minimum
function seems to suffice:Prints:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.minimum.html