我想知道是否有一种快速方法来计算 3D numpy 数组的所有点之间的欧几里得距离(A [N,3]
) 到第二个 3D numpy 数组的所有点 (B [M,3]
).
然后我应该得到一个数组C
这将是[N, M]
与数组点的所有距离A
到数组的点B
然后使用np.min()
沿指定轴获取距集合点的所有最小距离A
到设定点B
.
到目前为止,这是我完成实施的方式:
distances = np.repeat(9999, len(A))
for i, point in enumerate(A):
min_distance = np.min(np.sqrt(np.sum(np.square(point - B), axis=1)))
distances[i] = min_distance
有什么办法可以摆脱 for 循环吗...?
提前致谢 :)
import numpy as np
# arrays with xyz coordinates of all points
a = np.asarray([[xa1,ya1,za1],...,[xan,yan,zan]])
b = np.asarray([[xb1,yb1,zb1],...,[xbn,ybn,zbn]])
# reshaping to be able to calculate the distance matrix
a_reshaped = a.reshape(a.shape[0], 1, 3)
b_reshaped = b.reshape(1, b.shape[0], 3)
"""calculation of all distances between all points - creates a
len(a) x len(b) matrix"""
distance = np.sqrt(np.sum((a_reshaped - b_reshaped)**2, axis=2))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)