Calculate the Euclidean distance between two vectors. - CSharp System

CSharp examples for System:Math Vector

Description

Calculate the Euclidean distance between two vectors.

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections.Generic;
using System;/*from ww  w .  j a va 2  s  .c om*/

public class Main{
        /// <summary>
        /// Calculate the Euclidean distance between two vectors.
        /// </summary>
        /// <param name="p1">The first vector.</param>
        /// <param name="p2">The second vector.</param>
        /// <returns>The distance.</returns>
        public static double EuclideanDistance(double[] p1, double[] p2)
        {
            double sum = 0;
            for (int i = 0; i < p1.Length; i++)
            {
                double d = p1[i] - p2[i];
                sum += d * d;
            }
            return Math.Sqrt(sum);
        }
}

Related Tutorials