Created
August 5, 2017 16:59
-
-
Save ramiresnas/f31b9e4d21835c82816ae766f175c268 to your computer and use it in GitHub Desktop.
Euclidean distance of all points of one matrix
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
import static java.lang.Math.sqrt; | |
import static java.lang.Math.pow; | |
import static java.lang.System.out; | |
public class { | |
public static void main(String[] arg) { | |
double[][] X = { { 9, 2 }, { 1, 4 }, { 9, 6 }, { 2, 8 } }; | |
int row = X.length; | |
double[] distances = new double[(row * (row - 1)) / 2]; | |
int index= 0;matrix | |
for (int i = 0; i <row ; i++) { | |
for (int j = i + 1;j<row;j++) { | |
distances[index++] = euclideanDist(X[i], X[j]); | |
} | |
} | |
for (double d : distances) { | |
out.printf("[%2.3f]", d); | |
} | |
} | |
public static double euclideanDist(double[] p1, double[] p2) { | |
return sqrt( pow(p1[0]-p2[0],2) + pow(p1[1] - p2[1],2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment