numpy 如何在Python中计算两个大小相同的数值矩阵之间的元素最小值/最大值?[复制]

vddsk6oq  于 2022-11-10  发布在  Python
关注(0)|答案(2)|浏览(210)

这个问题在这里已经有答案

How to conditionally combine two numpy arrays of the same shape(2个答案)
两年前就关闭了。
我试过这样做:

  1. A = np.asmatrix(np.random.rand(4,1))
  2. B = np.asmatrix(np.random.rand(4,1))
  3. C = np.min(A, B)

让A和B如下所示:

  1. A = [[0.13456968]
  2. [0.80465702]
  3. [0.08426155]
  4. [0.85041178]]
  5. B = [[0.64932459]
  6. [0.77806739]
  7. [0.15517366]
  8. [0.10992883]]

我想要C,如下所示:

  1. C = [[0.13456968]
  2. [0.77806739]
  3. [0.08426155]
  4. [0.10992883]]

但这会产生以下错误:

  1. TypeError: only integer scalar arrays can be converted to a scalar index
2guxujil

2guxujil1#

您需要使用Minimum()而不是min():

  1. A = np.asmatrix(np.random.rand(4,1))
  2. B = np.asmatrix(np.random.rand(4,1))
  3. C = np.minimum(A, B)
tvz2xvvm

tvz2xvvm2#

尝试将其替换为

  1. C = np.minimum.reduce([A, B])

相关问题