首页 > 解决方案 > numpy.maximum() 在两个 3-D 数组上

问题描述

我正在寻找 numpy 方式来比较两个 3-D 数组并返回一个新的 3-D 数组,其中包含基于数组的给定索引的元素最大值。例如,我的值在 [y][x][7] 中:

output = numpy.zeros((16, 16, 8))
# we want to compare the values in [y,x,7] and only keep the element with max value
for x in range(array_min.shape[1]):
  for y in range(array_min.shape[0]):                
    if abs(array_min[y][x][7]) > array_max[y][x][7]:
      output[y][x] = array_min[y][x]
    else:
      output[y][x] = array_max[y][x]

标签: pythonnumpymax

解决方案


如果我理解正确,您只需要比较第 3 维的特定索引。在这种情况下,numpy 有一个内置函数,您可以将循环替换为:

output[:,:,7] = np.maximum(array_min[:,:,7], array_max[:,:,7])

推荐阅读