我编写了一段代码,能够在坐标系中产生特定宽度和长度范围内所需数量的点。我该如何使用欧几里得方法来计算并制成这些点的距离矩阵?
import random
npoints = int(input("Type the npoints:"))
width = float(input("Enter the Width you want:"))
height = float(input("Enter the Height you want:"))
sample = []
for _ in range(npoints):
sample.append((width * random.random(), height * random.random()))
print(*[f"({w:.2f}, {h:.2f})" for w, h in sample], sep=', ')
输出是:
Type the npoints:4
Enter the Width you want:10
Enter the Height you want:10
(8.52, 3.73), (9.69, 6.87), (8.20, 6.14), (4.18, 0.76)
Process finished with exit code 0
如何像这个例子一样,使用随机点创建距离矩阵:
非常感谢您的帮助。
itertools.combinations(list_of_points, 2)
进行成对分组,并对每一对应用欧几里得距离。使用numpy
可以更加流畅地进行元素级操作。 - cards