Skip to content

Instantly share code, notes, and snippets.

@pmeier
Created October 30, 2024 13:51
Show Gist options
  • Save pmeier/a61e5cbfff3e67130d01194068288534 to your computer and use it in GitHub Desktop.
Save pmeier/a61e5cbfff3e67130d01194068288534 to your computer and use it in GitHub Desktop.
Haversine distance matrix numpy
import numpy as np
def haversine_distance_matrix(lats, longs):
lats = np.radians(lats.reshape(-1, 1))
longs = np.radians(longs.reshape(1, -1))
return 2 * np.arcsin(np.sqrt((1 - np.cos(lats - lats.T) + np.cos(lats) * np.cos(lats.T) * (1 - np.cos(longs - longs.T))) / 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment