I've got a list of 8 coordinates stored as list of lists:
coordinates = [[47.2486, -1.54806],
[43.5656, 1.47417],
[48.3592, -4.57],
[48.1439, 17.1097],
[39.6275, 140.198],
[30.0458, 31.2625],
[38.9371, -77.0869],
[33.9, 35.4823]]
With the following script, I seek to output a matrix of coordinates:
import numpy
from scipy.spatial.distance import pdist
coordinates_array = numpy.array(coordinates)
dist_array = pdist(coordinates_array)
dist_matrix = numpy.reshape(dist_array, newshape=(len(coordinates), len(coordinates)))
However, I get an ValueError: total size of new array must be unchanged
. Why is that?
The distance matrix between the shapes, D∈R+N×N, is calculated using the Adjacent Entries Distance between the self functional maps, where N is the number of the shapes in the benchmark (94)Dij=DAE(Ci,Cj)i,j∈{1… N}.
This length can be computed with the help of Pythagora's theorem: dist = sqrt((x2-x1)^2 + (y2-y1)^2) . This is known as the Euclidian distance between the points.
The Distance Matrix API provides travel distance and time for a matrix of origins and destinations, and consists of rows containing duration and distance values for each pair. Distance Matrix is available in several forms: as a standalone API. as part of the client-side Maps JavaScript API.
You can use the math. dist() function to get the Euclidean distance between two points in Python. For example, let's use it the get the distance between two 3-dimensional points each represented by a tuple.
As explained in the scipy documentation, use scipy.spatial.distance.squareform
to convert the condensed distance matrix returned by pdist
to a square distance matrix,
from scipy.spatial.distance import squareform
dist_matrix = squareform(dist_array)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With