Skip to content

Instantly share code, notes, and snippets.

@joninsky
Created January 11, 2019 00:50
Show Gist options
  • Save joninsky/c7698dc7deab8baf79a86a6b6b1a0251 to your computer and use it in GitHub Desktop.
Save joninsky/c7698dc7deab8baf79a86a6b6b1a0251 to your computer and use it in GitHub Desktop.
Euclidean Distance between two arrays of Double values in Swift
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