Skip to content

Instantly share code, notes, and snippets.

@tlancon
Created May 17, 2020 04:19
Show Gist options
  • Save tlancon/b8a8546ba9476135822e27d310233cb9 to your computer and use it in GitHub Desktop.
Save tlancon/b8a8546ba9476135822e27d310233cb9 to your computer and use it in GitHub Desktop.
Calculate the distance between X, Y points saved arrays using NumPy's diff() function without a loop.
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([4, 5, 6, 7, 8, 9])
displacement = np.sqrt(np.diff(x, prepend=x[0])**2 + np.diff(y, prepend=y[0])**2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment