Created
January 11, 2019 00:50
-
-
Save joninsky/c7698dc7deab8baf79a86a6b6b1a0251 to your computer and use it in GitHub Desktop.
Euclidean Distance between two arrays of Double values in Swift
This file contains 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
func euclideanDistance(first: [Double], second: [Double]) -> Double { | |
guard first.count == second.count else { | |
fatalError("Vector space is not the same") | |
} | |
var result: Double = 0 | |
for i in 0..<first.count { | |
result += pow(first[i] - second[i], 2.0) | |
} | |
return sqrt(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment