Element-wise array maximum function in NumPy (more than two arrays) Element-wise array maximum function in NumPy (more than two arrays) numpy numpy

Element-wise array maximum function in NumPy (more than two arrays)


With this setup:

>>> A = np.array([0,1,2])>>> B = np.array([1,0,3])>>> C = np.array([3,0,4])

You can either do:

>>> np.maximum.reduce([A,B,C])array([3, 1, 4])

Or:

>>> np.vstack([A,B,C]).max(axis=0)array([3, 1, 4])

I would go with the first option.


You can use reduce. It repeatedly applies a binary function to a list of values...

For A, B and C given in question...

np.maximum.reduce([A,B,C]) array([3,1,4])

It first computes the np.maximum of A and B and then computes the np.maximum of (np.maximum of A and B) and C.


You can also use:

np.column_stack([A, B, C]).max(axis=1)

The result is the same as the solutions from the other answers.

I use Pandas more heavily than NumPy so for me it's easier to think of 1D arrays as something similar to Pandas Series. The above would be equivalent to:

pd.concat([A, B, C], axis=1).max(axis=1)