【发布时间】:2019-05-06 03:54:21
【问题描述】:
我在 Python 中有一个 3D numpy 浮点值数组。 我需要检索半径为 r 的球体中的所有元素 一个中心点 P(x, y, z)。然后,我想将一个函数应用于球体点 更新它们的值并需要到中心点的距离来执行此操作。我做了很多次这些步骤 大半径值,所以我想有一个同样有效的解决方案 尽可能。
我当前的解决方案只检查球体边界框中的点, 如此处所示:Using a QuadTree to get all points within a bounding circle。 代码草图如下所示:
# P(x, y, z): center of the sphere
for k1 in range(x - r, x + r + 1):
for k2 in range(y - r, y + r + 1):
for k3 in range(z - r, z + r + 1):
# Sphere center - current point distance
dist = np.sum((np.array([k1, k2, k3]) - np.array([x, y, z])) ** 2)
if (dist <= r * r):
# computeUpdatedValue(distance, radius): function that computes the new value of the matrix in the current point
newValue = computeUpdatedValue(dist, r)
# Update the matrix
mat[k1, k2, k3] = newValue
但是,我认为应用蒙版来检索点,然后, 基于距离以矢量化方式更新它们更有效。 我已经看到如何应用循环内核 (How to apply a disc shaped mask to a numpy array?), 但我不知道如何在每个掩码元素上有效地应用函数(取决于索引)。
【问题讨论】:
标签: python numpy vectorization mask