Combining two numpy arrays to form an array with the largest value from each array
You can use np.maximum
to compute the element-wise maximum of the two arrays:
>>> np.maximum(a, b)array([[ 0. , 0. , 0.5], [ 0.5, 0.5, 0.5], [ 0.5, 0.1, 0. ]])
This works with any two arrays, as long as they're the same shape or one can be broadcast to the shape of the other.
To modify the array a
in-place, you can redirect the output of np.maximum
back to a
:
np.maximum(a, b, out=a)
There is also np.minimum
for calculating the element-wise minimum of two arrays.
You are looking for the element-wise maximum.
Example:
>>> np.maximum([2, 3, 4], [1, 5, 2])array([2, 5, 4])
http://docs.scipy.org/doc/numpy/reference/generated/numpy.maximum.html