Java Euclidean Distance euclideanDistance(double[] vector1, double[] vector2)

Here you can find the source of euclideanDistance(double[] vector1, double[] vector2)

Description

Returns Euclidean distance between the two vectors

License

LGPL

Parameter

Parameter Description
vector1 a parameter
vector2 a parameter

Declaration

public static double euclideanDistance(double[] vector1,
        double[] vector2) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*from  w  w  w. j  a v a2  s  . co  m*/
     * Returns Euclidean distance between the two vectors
     * 
     * @param vector1
     * @param vector2
     * @return
     */
    public static double euclideanDistance(double[] vector1,
            double[] vector2) {
        double sum = 0;

        for (int i = 0; i < vector1.length; i++) {
            sum += (vector1[i] - vector2[i]) * (vector1[i] - vector2[i]);
        }

        return Math.sqrt(sum);
    }

    /**
     * Returns Euclidean distance between the two vectors ignoring null elements of vector1.
     * 
     * @param vector1
     * @param vector2 
     * @return
     */
    public static double euclideanDistance(Double[] vector1,
            double[] vector2) {
        double sum = 0;

        for (int i = 0; i < vector1.length; i++) {
            if (vector1[i] != null)
                sum += (vector1[i] - vector2[i])
                        * (vector1[i] - vector2[i]);
        }

        return Math.sqrt(sum);
    }
}

Related

  1. euclideanDistance(double[] coord1, double[] coord2)
  2. euclideanDistance(double[] data, double[] pattern)
  3. euclideanDistance(double[] l1, double[] l2, boolean weighted)
  4. euclideanDistance(double[] p, double[] q)
  5. euclideanDistance(double[] vector)
  6. euclideanDistance(float[] points, int p1, int p2, boolean isDisp, double width, double height)
  7. euclideanDistance(int i0, int j0, int i1, int j1)
  8. euclideanDistance(int startX, int startY, int startZ, int endX, int endY, int endZ)
  9. euclideanDistanceNorm(float[] x, float[] y)