Java Euclidean Distance euclideanDist(final double[] vec1, final double[] vec2, int n)

Here you can find the source of euclideanDist(final double[] vec1, final double[] vec2, int n)

Description

Compute the euclidean distance between 2 vectors.

License

Open Source License

Parameter

Parameter Description
vec1 a real vector
vec2 another real vector
n length of two vectors

Return

the euclidean distance between vec1 and vec2

Declaration

public static double euclideanDist(final double[] vec1, final double[] vec2, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w  w w. j av a  2 s.c o m
     * Compute the euclidean distance between 2 vectors.
     * Preconditions : vec1.length == vec2.length == n
     * @param vec1    a real vector
     * @param vec2    another real vector
     * @param n       length of two vectors
     * @return the euclidean distance between vec1 and vec2
     */
    public static double euclideanDist(final double[] vec1, final double[] vec2, int n) {
        assert vec1 != null;
        assert vec2 != null;
        assert vec1.length == n;
        assert vec2.length == n;

        double dist = 0;
        for (int i = 0; i < n; i++) {
            dist += Math.pow(vec1[i] - vec2[i], 2);
        }
        return Math.sqrt(dist);
    }
}

Related

  1. euclidean(double[] features1, double[] features2)
  2. euclidean(double[] x1, double[] x2)
  3. euclidean(double[] xvalues, double[] yvalues, int[] include)
  4. euclidean_distance(double[] vec1, double[] vec2)
  5. euclideanDist(double[] ePointOne, double[] ePoint2)
  6. euclideanDist(float[] v1, float[] v2)
  7. euclideanDistance(double instance1[], double instance2[])
  8. euclideanDistance(double x1, double y1, double x2, double y2)
  9. euclideanDistance(double x1, double y1, double x2, double y2)