Skip to content

Instantly share code, notes, and snippets.

@alexandreprates
Last active January 9, 2020 19:00
Show Gist options
  • Save alexandreprates/2501d329691779cc8103b9d9c2d3018b to your computer and use it in GitHub Desktop.
Save alexandreprates/2501d329691779cc8103b9d9c2d3018b to your computer and use it in GitHub Desktop.
Euclidean distance between two arrays
# Calculate the euclidean distance between two arrays
# Example:
# euclidean_distance([1,2], [3,4]) #=> 2.8284271247461903
# euclidean_distance([1,2,3], [4,5,6]) #=> 5.196152422706632
def euclidean_distance(vector1, vector2)
Math.sqrt(vector1.zip(vector2).collect { |a,b| (a - b)**2 }.sum)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment