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

Home » Public Forums » archive » Re: Distance between two sets of datapoints
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: Distance between two sets of datapoints [message #70223] Fri, 26 March 2010 15:11
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
Thanks Ken,

It is definitely much much faster (from 30 minutes for looping only to
1 minute). It is difficult to characterise the speedup entirely
because I was able to add a lot more of the calculations inside the
loop, na is about 700 times larger than nb. I think I must have done
something stupid the first time I tried this and looped over the other
dimension hence my confusion.

Cheers,
Max

On Mar 27, 12:11 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
> In article
> < eb0d2a64-900b-4c9e-9599-699dcd3da...@k4g2000prh.googlegroups .com >,
>  Maxwell Peck <maxjp...@gmail.com> wrote:
>
>> Thanks Ken, I'll give it a shot. I had tried a similar loop (I
>> thought) but it seemed to be very slow that is why i was looking at
>> vectorising it similar to distance_measure.
>
>>> FOR i = 0, nb-1 DO dist[0,i] = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
>
> This should vectorize reasonably well if na is large and nb is
> not too large.
>
> Ken
Re: Distance between two sets of datapoints [message #70228 is a reply to message #70223] Fri, 26 March 2010 06:11 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article
<eb0d2a64-900b-4c9e-9599-699dcd3da17b@k4g2000prh.googlegroups.com>,
Maxwell Peck <maxjpeck@gmail.com> wrote:

> Thanks Ken, I'll give it a shot. I had tried a similar loop (I
> thought) but it seemed to be very slow that is why i was looking at
> vectorising it similar to distance_measure.
>>
>> FOR i = 0, nb-1 DO dist[0,i] = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)

This should vectorize reasonably well if na is large and nb is
not too large.

Ken
Re: Distance between two sets of datapoints [message #70229 is a reply to message #70228] Fri, 26 March 2010 06:16 Go to previous message
Gray is currently offline  Gray
Messages: 253
Registered: February 2010
Senior Member
On Mar 25, 3:38 pm, Maxwell Peck <maxjp...@gmail.com> wrote:
> Thanks Ken, I'll give it a shot. I had tried a similar loop (I
> thought) but it seemed to be very slow that is why i was looking at
> vectorising it similar to distance_measure.
>
> Thanks
> Max
>
> On Mar 26, 6:04 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
>
>
>
>> In article <k-bowman-EEB2FB.09554725032...@news.tamu.edu>,
>>  "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
>
>>> FOR i = 0, nb-1 DO BEGIN
>>>    d = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
>>>    dist[0,i] = d
>>> ENDFOR
>
>> I just realized this could be simplified to
>
>> FOR i = 0, nb-1 DO dist[0,i] = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
>
>> which removes some unneeded memory access.
>
>> Ken

Take a look at JD Smith's match_2d (http://tir.astro.utoledo.edu/idl/
match_2d.pro), which uses histogram to quickly find distances between
lists of coordinates (and then matches them, which you don't need).
Re: Distance between two sets of datapoints [message #70238 is a reply to message #70228] Thu, 25 March 2010 12:38 Go to previous message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
Thanks Ken, I'll give it a shot. I had tried a similar loop (I
thought) but it seemed to be very slow that is why i was looking at
vectorising it similar to distance_measure.

Thanks
Max

On Mar 26, 6:04 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
> In article <k-bowman-EEB2FB.09554725032...@news.tamu.edu>,
>  "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
>
>
>
>> FOR i = 0, nb-1 DO BEGIN
>>    d = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
>>    dist[0,i] = d
>> ENDFOR
>
> I just realized this could be simplified to
>
> FOR i = 0, nb-1 DO dist[0,i] = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
>
> which removes some unneeded memory access.
>
> Ken
Re: Distance between two sets of datapoints [message #70240 is a reply to message #70238] Thu, 25 March 2010 12:04 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <k-bowman-EEB2FB.09554725032010@news.tamu.edu>,
"Kenneth P. Bowman" <k-bowman@null.edu> wrote:

>
> FOR i = 0, nb-1 DO BEGIN
> d = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
> dist[0,i] = d
> ENDFOR

I just realized this could be simplified to

FOR i = 0, nb-1 DO dist[0,i] = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)

which removes some unneeded memory access.

Ken
Re: Distance between two sets of datapoints [message #70254 is a reply to message #70240] Thu, 25 March 2010 07:55 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article
<4707b3ed-9e43-4863-977f-7fd091e62868@a4g2000prb.googlegroups.com>,
Maxwell Peck <maxjpeck@gmail.com> wrote:

> Hi All,
>
> I have two sets of data points, inputa and inputb, inputa has many
> more data points than the other. I need to find the distance between
> each point in inputa and all the points in inputb (hopefully without
> loops). At the moment I am using parts of distance_measure.pro to
> generate the full set of distances and subsetting indexes as required.
> The code follows. I can't see an easy way of generating the indexes
> though so that only the pairs i want are calculated (i.e. not
> calculating between inputb/inputb or inputa/inputa indexes).
>
> inputa and inputb can be very very large so I don't think i can use a
> matrix approach to do it vectorially and looping seems awfully slow.
>
> An alternative approach or other suggestions would be appreciated.
>
> Regards
>
> Max
>
>
> inputa = findgen(2,10)
> inputb = findgen(2,2)
>
> t = [[inputa],[inputb]]
> m=n_elements(t)/2
> n = m*(m-1)/2
>
> ii = 0L
> index0 = LINDGEN(m - 1) + 1 ; work array
> index1 = LONARR(n, /NOZERO)
> index2 = LONARR(n, /NOZERO)
>
> for i=0,m-2 do begin
> n1 = m - (i+1)
> index1[ii:ii+n1-1] = i
> index2[ii] = index0[0:n1-1] + i
> ii += n1
> endfor
>
> diff = abs(t[*,index1] - t[*,index2])
> res = sqrt(TOTAL(diff^2, 1))

If I understand you right, don't you want to do this?

na = 10
nb = 2
xa = FINDGEN(na)
ya = FINDGEN(na)
xb = FINDGEN(nb)
yb = FINDGEN(nb)

dist = FLTARR(na, nb)

FOR i = 0, nb-1 DO BEGIN
d = SQRT((xb[i] - xa)^2 + (yb[i] - ya)^2)
dist[0,i] = d
ENDFOR

I changed the notation to make the indexing simpler.

This computes the Cartesian distance between all pairs of
points in a and b.

You should loop over the smaller of na or nb.

Ken Bowman
Re: Distance between two sets of datapoints [message #70256 is a reply to message #70254] Thu, 25 March 2010 03:46 Go to previous message
Maxwell Peck is currently offline  Maxwell Peck
Messages: 61
Registered: February 2010
Member
That should say I am generating the full set of indices and then
subsetting.
Thanks
On Mar 25, 9:33 pm, Maxwell Peck <maxjp...@gmail.com> wrote:
> Hi All,
>
> I have two sets of data points, inputa and inputb, inputa has many
> more data points than the other. I need to find the distance between
> each point in inputa and all the points in inputb (hopefully without
> loops). At the moment I am using parts of distance_measure.pro to
> generate the full set of distances and subsetting indexes as required.
> The code follows. I can't see an easy way of generating the indexes
> though so that only the pairs i want are calculated (i.e. not
> calculating between inputb/inputb or inputa/inputa indexes).
>
> inputa and inputb can be very very large so I don't think i can use a
> matrix approach to do it vectorially and looping seems awfully slow.
>
> An alternative approach or other suggestions would be appreciated.
>
> Regards
>
> Max
>
> inputa = findgen(2,10)
> inputb = findgen(2,2)
>
> t = [[inputa],[inputb]]
> m=n_elements(t)/2
> n = m*(m-1)/2
>
> ii = 0L
> index0 = LINDGEN(m - 1) + 1   ; work array
> index1 = LONARR(n, /NOZERO)
> index2 = LONARR(n, /NOZERO)
>
> for i=0,m-2 do begin
>   n1 = m - (i+1)
>   index1[ii:ii+n1-1] = i
>   index2[ii] = index0[0:n1-1] + i
>   ii += n1
> endfor
>
> diff = abs(t[*,index1] - t[*,index2])
> res = sqrt(TOTAL(diff^2, 1))
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Highlight default button
Next Topic: IDL licensing

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

Current Time: Wed Oct 08 11:45:39 PDT 2025

Total time taken to generate the page: 0.00655 seconds