I want to generate a matrix that stores the distance between every point with every other point. I want to be able to access this distance value in the matrix using the two coordinates.
As in the image below, I want to be able to access the distance using the points as the index.
matrix[point a, point b] = distance between two points
One solution is to use the pandas
module.
scipy.spatial.distance.cdist
df["[x, y]"]
iloc
on a columnFull code + illustration
# import modules
import pandas as pd
from scipy.spatial.distance import cdist
# input points
points = [[1, 2], [2, 3], [3, 4], [5, 6], [9, 10]]
# Create dataframe
df = pd.DataFrame(cdist(points, points),
columns=[str(p) for p in points],
index=[str(p) for p in points])
print(df)
# [1, 2] [2, 3] [3, 4] [5, 6] [9, 10]
# [1, 2] 0.000000 1.414214 2.828427 5.656854 11.313708
# [2, 3] 1.414214 0.000000 1.414214 4.242641 9.899495
# [3, 4] 2.828427 1.414214 0.000000 2.828427 8.485281
# [5, 6] 5.656854 4.242641 2.828427 0.000000 5.656854
# [9, 10] 11.313708 9.899495 8.485281 5.656854 0.000000
# select column "[2, 3]"
print(df["[2, 3]"])
# [1, 2] 1.414214
# [2, 3] 0.000000
# [3, 4] 1.414214
# [5, 6] 4.242641
# [9, 10] 9.899495
# get distance between point [2 3] and [1 2]
print(df["[2, 3]"].loc["[1, 2]"])
# 1.4142135623730951
Hope that helps