Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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