Last active
January 9, 2020 19:00
-
-
Save alexandreprates/2501d329691779cc8103b9d9c2d3018b to your computer and use it in GitHub Desktop.
Euclidean distance between two arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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