Skip to content

Instantly share code, notes, and snippets.

@petergi
Last active December 27, 2023 22:12
Show Gist options
  • Select an option

  • Save petergi/4e002f0b3426c2a1ae516746ab2aee29 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/4e002f0b3426c2a1ae516746ab2aee29 to your computer and use it in GitHub Desktop.
Calculates the distance between two vectors
/**
* Calculates the distance between two vectors.
*
* @param {Array} x - The first vector.
* @param {Array} y - The second vector.
* @return {number} The distance between the two vectors.
*/
function vectorDistance(x, y) {
return Math.sqrt(x.reduce((acc, val, i) => acc + (val - y[i]) ** 2, 0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment