comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Euclidean distance
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Euclidean distance [message #11637] Thu, 21 May 1998 00:00
Caesar E. Ordonez is currently offline  Caesar E. Ordonez
Messages: 2
Registered: May 1998
Junior Member
<HTML>
Imanol Echave wrote:
<BLOCKQUOTE TYPE=CITE>Hi:

<P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; I'd like to program the following
routine: giving two set of vectors with n and
<BR>m vectors respectively (a matrix of size pxn and another of size pxm),
I want to
<BR>obtain a matrix of size nxm where the element (i,j) is the euclidean
distance
<BR>from first set's vector i to second set's vector j. I know the solution
using
<BR>loops, but exists it a different way without loops?</BLOCKQUOTE>
Here's my (quick and dirty) solution for two vectors.

<P>&nbsp;&nbsp;&nbsp; function edist, a, b
<BR>&nbsp;&nbsp;&nbsp; na = n_elements( a )
<BR>&nbsp;&nbsp;&nbsp; nb = n_elements( b )
<BR>&nbsp;&nbsp;&nbsp; ; print, na, nb
<BR>&nbsp;&nbsp;&nbsp; aa = a
<BR>&nbsp;&nbsp;&nbsp; bb = b
<BR>&nbsp;&nbsp;&nbsp; aa[*] = 1
<BR>&nbsp;&nbsp;&nbsp; bb[*] = 1
<BR>&nbsp;&nbsp;&nbsp; c1 = a^2#bb
<BR>&nbsp;&nbsp;&nbsp; c2 = aa#b^2
<BR>&nbsp;&nbsp;&nbsp; c = c1+c2
<BR>&nbsp;&nbsp;&nbsp; c = sqrt(c)
<BR>&nbsp;&nbsp;&nbsp; ;Check with loops
<BR>&nbsp;&nbsp;&nbsp; ;d=dblarr(na,nb)
<BR>&nbsp;&nbsp;&nbsp; ;for k=0,nb-1 do begin
<BR>&nbsp;&nbsp;&nbsp; ;for j=0,na-1 do begin
<BR>&nbsp;&nbsp;&nbsp; ; d[j,k]=sqrt(a[j]^2 + b[k]^2)
<BR>&nbsp;&nbsp;&nbsp; ;endfor
<BR>&nbsp;&nbsp;&nbsp; ;endfor
<BR>&nbsp;&nbsp;&nbsp; ;print,' '
<BR>&nbsp;&nbsp;&nbsp; ;print,c-d
<BR>&nbsp;&nbsp;&nbsp; return, c
<BR>&nbsp;&nbsp;&nbsp; end

<P>You can easily generalize to matrices by inserting code, before
<BR>the second line, that checks dimensions of a and b, and transforming
<BR>a and/or b to one-dimensional array via reform function.
<PRE>--&nbsp;
Caesar E. Ordonez, Ph.D.
Department of Medical Physics
Rush-Presbyterian-St. Luke's Medical Center
ordonez@spect2.rad.rpslmc.edu</PRE>
&nbsp;</HTML>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Image subtraction
Next Topic: Re: Conundrum

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:50:47 PDT 2025

Total time taken to generate the page: 0.00556 seconds