def DistanceMatrix(coords):
'''Take a set of coordinates and calculate a matrix of
Euclidean distances between the points.'''
# we can assume that xs and ys are the same length
stops = coords.shape[1]
distMat = scipy.zeros((stops, stops))
# this will be symmetric, so we only need to calculate
# the upper triangular
for i in range(stops):
for j in range(i + 1, stops):
xdist = coords[0, i] - coords[0, j]
ydist = coords[1, i] - coords[1, j]
distMat[i, j] = math.sqrt(xdist**2 + ydist**2)
# add the transpose to make it symmetric
distMat = distMat + distMat.transpose()
return distMat
评论列表
文章目录