# Euclidean distance in Octave

I would like to know if there is a quick way to compute the Euclidean distance of two vectors in Octave. It seems that there is no special function for that, so should I just use the formula with sqrt?

• This may have been down-voted because this is a basic question you should be able to find the answer for by looking at Octave documentation or doing an appropriate internet search. However, I do see that searching for "octave Euclidean" or "octave Euclidean distance" does not yield useful results nearly as quickly as "octave norm". – Elaine Hale Aug 13 '13 at 20:57
• suggestion : next time, Google something like "Matlab distance between two vectors". The Mathworks has centralized documentation which is easy to find (there is an official Octave documentation manual which you should find, but it may take a little work to find it). For something this simple, it is probable that a function with the same name in Octave does the same thing. – Stefan Smith Oct 6 '13 at 0:15
• The stats on this question speak for themselves. – boatcoder Apr 1 '17 at 18:43

The Euclidean distance between two vectors is the two-norm of their difference, hence

d = norm( x1 - x2 , 2 );


should do the trick in Octave. Note that if the second argument to norm is omitted, the 2-norm is used by default.

You can also try distancePoints

http://octave.sourceforge.net/geometry/function/distancePoints.html

Here is a method to calculate the distance between an array of vectors (X) and a single vector (X(1, :) for example purposes):

distances = sqrt(sum((X - X(1, :)) .^ 2, 2));