Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
euclidean distance of vectors without loop??

Subject: euclidean distance of vectors without loop??

From: Krishna

Date: 20 Jul, 2007 09:42:18

Message: 1 of 3

>> a = [1 2 3; 4 5 6; 7 8 9]
>> b = [ 8 3 4; 5 4 6; 5 6 7]
>> d = mean(abs(a-b),1)

helps?

Krishna
http://dsplog.blogspot.com

Subject: euclidean distance of vectors without loop??

From: Ken Davis

Date: 20 Jul, 2007 07:25:01

Message: 2 of 3

"Krishna" <krishna.pillai@gmail.com> wrote in message
news:1184924538.758486.298140@z24g2000prh.googlegroups.com...
>>> a = [1 2 3; 4 5 6; 7 8 9]
>>> b = [ 8 3 4; 5 4 6; 5 6 7]
>>> d = mean(abs(a-b),1)
>
> helps?
>
> Krishna
> http://dsplog.blogspot.com
>

Euclidean distance is the square root of the sum of the squares of the
individual components of two vectors (points in Euclidean N-space).

Subject: euclidean distance of vectors without loop??

From: Peter Wittenberg

Date: 20 Jul, 2007 16:31:17

Message: 3 of 3

I believe what is desired here is how to calculate the Euclidean distance for a vector without using a do loop to run through all the indexes.

This can be achieved easily (it's one of the beauties of MATLAB) with the dot multiply command. In addition to the standard matrix mathematics, MATLAB offers a set of dot matrix commands that operate on matrices element by element.

Let me provide an example. Assume a and b are vectors (1 by N or perhaps N by 1). All that matters is that they have the same dimensions.

Then a-b is certainly the vector difference. Now we have to do the root sum square, and this is where the dot multiply is appropriate.

(a-b).*(a-b) is a vector of the squares of the elements. We just take the sum as

sum((a-b).*(a-b))

Now we have a single number and we're ready to take the square root, so

Euclidean distance = sqrt( sum((a-b).*(a-b)) )

You may also be able to use some variant of the norm command. Either way is fairly easy and does not require the user to do any loops.

Is this what you were after?

"Ken Davis" <kendavis@alum.mit.edu> wrote in message <F6E1E2BF45EC8E39C1F1576F139CF8B1@in.webcrossing.raydaftYaTP>...
> "Krishna" <krishna.pillai@gmail.com> wrote in message
> news:1184924538.758486.298140@z24g2000prh.googlegroups.com...
> >>> a = [1 2 3; 4 5 6; 7 8 9]
> >>> b = [ 8 3 4; 5 4 6; 5 6 7]
> >>> d = mean(abs(a-b),1)
> >
> > helps?
> >
> > Krishna
> > http://dsplog.blogspot.com
> >
>
> Euclidean distance is the square root of the sum of the squares of the
> individual components of two vectors (points in Euclidean N-space).
>
>

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us