Skip to content

Instantly share code, notes, and snippets.

@ramiresnas
Created August 5, 2017 16:59
Show Gist options
  • Save ramiresnas/f31b9e4d21835c82816ae766f175c268 to your computer and use it in GitHub Desktop.
Save ramiresnas/f31b9e4d21835c82816ae766f175c268 to your computer and use it in GitHub Desktop.
Euclidean distance of all points of one matrix
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