16
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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". $\endgroup$ – Elaine Hale Aug 13 '13 at 20:57
  • $\begingroup$ 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. $\endgroup$ – Stefan Smith Oct 6 '13 at 0:15
  • $\begingroup$ The stats on this question speak for themselves. $\endgroup$ – boatcoder Apr 1 '17 at 18:43
30
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

You can also try distancePoints

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

$\endgroup$
0
$\begingroup$

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));
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.