我正在实现一个简单的代码,它计算一个点和所有点之间的距离(x_a, y_a)
,并返回找到的最小距离。对 中的所有点重复此操作。list_A
(x_b, y_b)
list_B
list_A
我的MWE
代码之一:
# list_A points defined in array.
list_A = np.array([
[x_data_a, # x
y_data_a] # y
], dtype=float)
# list_B points defined in list.
list_B = [[x_data_b], [y_data_b]]
# Iterate through all data points in list_A
for ind, x_a in enumerate(list_A[0][0]):
y_a = list_A[0][1][ind]
# Iterate through all points in list_B.
dist_min = 1000.
for ind2, x_b in enumerate(list_B[0]):
y_b = list_B[1][ind2]
# Find distance between points.
dist = (x_a-x_b)**2 + (y_a-y_b)**2
if dist < dist_min:
# Update value of min distance.
dist_min = dist
print 'Min dist to (', x_a, y_a, '): ', dist_min
数据格式如下:
list_A = [[[1.2 2.3 1.5 2.3 5.8 4.6 9.1] [2.5 1.0 4.6 2.4 7.4 1.1 3.2]]]
list_B = [[1.4, 5.8, 7.9], [6.1, 1.2, 3.7]]
对于大列表/数组,这可能需要相当长的时间才能完成。这可以加速吗?