简体   繁体   English

numpy.ndarray中对应元素的最大值

[英]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. 我有两个numpy.ndarrays (例如A,B),并希望创建形状和尺寸相同的第三个(例如C),这样C中的每个元素都是A和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 : 怎么样np.where

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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