简体   繁体   中英

max of Corresponding Elements in a numpy.ndarray

This seems like it would be a really simple problem, but i haven't been able to find a solution so far.

I have two numpy.ndarrays (say A, B) and would like to create a third one (say C) of the same shape and dimensionality, such that each element in C is the maximum value of the corresponding elements in A and B.

What I've tried so far doesn't work, though to be honest, I haven't tried much (but I'm out of ideas)

In [173]: A
Out[173]: 
array([[  2.12752806e-314,   2.12752806e-314],
       [  2.16171674e-314,   6.32300944e+233]])

In [174]: B
Out[174]: 
array([[  2.13899304e-314,   2.13899304e-314],
       [  2.16168421e-314,   2.78136354e-309]])

In [175]: max(A, B)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-175-c06ce068ec08> in <module>()
----> 1 max(A, B)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

您正在寻找np.maximum(A,B)

How about np.where :

In [29]: where(A>B, A, B)
Out[29]: 
array([[  2.13899304e-314,   2.13899304e-314],
       [  2.16171674e-314,   6.32300944e+233]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM