【发布时间】:2017-11-25 22:15:12
【问题描述】:
我有一个这样的数组:tmp.shape = (128, 64, 64)
我沿128 轴计算所有零,如下所示:
nonzeros = np.count_nonzero(tmp, axis=0) // shape = (64, 64)
我有一个数组c.shape = (64, 64)
现在我想将 c 的值沿 128 轴添加到 tmp,但前提是 tmp 的值 > 0:
for i in range(tmp.shape[0]):
axis1 = tmp[i,:,:]
tmp[i, :, :] += (c / nonzeros) // only if tmp[i, :, :] > 0
这可以在短时间内完成吗?还是我必须使用多个循环? 我希望任何人都可以提出一个没有另一个循环的解决方案
这样的事情不起作用:
tmp[i, tmp > 0.0, tmp > 0.0] += (c / nonzeros)
IndexError: 数组索引过多
长版
for i in range(tmp.shape[0]):
for x in range(tmp.shape[1]):
for y in range(tmp.shape[2]):
pixel = tmp[i, x, y]
if pixel != 0:
pixel += (c[x,y] / nonzeros[x,y])
【问题讨论】:
-
发布的解决方案是否适合您?
-
是的,对不起,我最近很忙,所以没有马上回答。
标签: python numpy multidimensional-array vectorization