python - 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]
解决方案
如果我理解正确,您只需要比较第 3 维的特定索引。在这种情况下,numpy 有一个内置函数,您可以将循环替换为:
output[:,:,7] = np.maximum(array_min[:,:,7], array_max[:,:,7])
推荐阅读
- uml - 地铁铁路UML类图
- javascript - 每秒(近 1800 秒)调用 javascript 函数 setTimeout 和 setInterval 有滞后问题
- python - 如何从python中的数据集中删除某些数值?
- reactjs - 混合图像背景的底部
- python - 找出列表元素所有可能方式的差异
- python - 使用 utf-8 进行 .epub 转换的输出格式不正确
- python - 如何检测两个线段(在 3d 空间中)是否相交?
- elasticsearch - ElasticSearch 快照:部分状态的原因
- javascript - HTML 可以从 URL 播放流媒体视频吗?
- java - Selenium Java - 将图像从 URL 复制到剪贴板并将其粘贴为图像