Calculating Euclidean distance in Ruby

Written by Keith McDonnell. Last updated on Friday, November 06, 2009.

Simply put, Euclidean distance is the shortest distance between two points. The kind of distance you would measure with a ruler. e.g. Calculate distance C in the diagram below.

Euclidean distance formula

Euclidean distance is calculated using the following formula:

For example, the distance between points P (1,2) and Q (3,4) is:

√ (P 1 – Q 1 ) 2 + (P 2 – Q 2 ) 2
= √ (1-3) 2 + (2-4) 2
= √ (-2) 2 + (-2) 2
= √ 4 + 4 ≈ 2.828

To calculate the distance in multiple dimensions, simply sum the square of each extra point. For example, The distance between points P (1,2,3) and Q (4,5,6) is:

√ (P 1 – Q 1 ) 2 + (P 2 – Q 2 ) 2 + (P 3 – Q 3 ) 2
= √ (1-4) 2 + (2-5) 2 + (3-6) 2
= √ (-3) 2 + (-3) 2 (-2) 2 + (-3) 2
= √ 9 + 9 + 9
= √ 27 ≈ 5.196

Ruby implementation

To recap, we want to implement the following formula for Euclidean distance in Ruby.

The main point to note is that the function should take two arrays, each with an arbitrary length. Each element of the array represents a point in a multidimensional space. For the sake of clarity, this example excludes error handling (e.g. arrays of differing lengths ).

References

Futher reading

You can also use Euclidean distance to:

I’m working through the examples in the Collective Intelligence book by Segaran. You can find them at my Collective Intelligence github repository

If you'd like to discuss this article, you can send me an email keith@dancingtext.com and/or publish an article online and link back to this page.