Created
July 20, 2019 09:10
-
-
Save engleangs/df7d7df2eb37e56933a963af6edc1b0e to your computer and use it in GitHub Desktop.
DistanceCalculator using Euclidean formula
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
package com.demo.knn; | |
/** | |
* @author Engleang | |
* Distance calculator using Euclidean fomula | |
* | |
* */ | |
public final class DistanceCalculator { | |
public final static double getDistance(double[] data1, double[] data2) { | |
if (data1.length != data2.length) { | |
throw new IllegalArgumentException(" Data length mismatched ! "); | |
} | |
double sum = 0; | |
for(int i =0;i<data1.length;i++) { | |
sum += Math.pow( data1[i] - data2[i],2); | |
} | |
return Math.sqrt( sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment