Compute the Euclidean distance between samples. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Compute the Euclidean distance between samples.

Demo Code


//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//  w w w.  ja v  a2 s.c  om
     * Compute the Euclidean distance between samples.
     * @param i1 First sample.
     * @param i2 Second Sample.
     * @return  Return the Euclidean distance between samples.
     */
    public static double euclideanDistance(Collection<Double> i1,
            Collection<Double> i2) {
        double dist = 0.0;
        double value;

        assert (i1.size() == i2.size()) : "Dimensions must agree.";

        Iterator<Double> it1 = i1.iterator();
        Iterator<Double> it2 = i2.iterator();

        while (it1.hasNext() && it2.hasNext()) {
            value = it1.next() - it2.next();

            dist += value * value;
        }//while

        return Math.sqrt(dist);
    }
}

Related Tutorials