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

Home » Public Forums » archive » Re: Array comparison part 2
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: Array comparison part 2 [message #32380] Fri, 04 October 2002 13:04
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Thu, 03 Oct 2002 09:33:24 -0700, Dick Jackson wrote:

> "Sean Raffuse" <sean@me.wustl.edu> wrote in message
> news:anhkqb$ipf$1@newsreader.wustl.edu...
>> Thanks for all the help on my first question. I now have a related
>> question.
>>
>> What is the best (read: fastest) way to do the following:
>>
>> I have an array of coordinates, A = intarr(2,25) and I have another
>> array of a specific location, B = [125,1043]
>>
>> I would like to determine if location B is one of the coordinates in
> A. I
>> need to know if A[*,?] = 125, 1043
>>
>> Is it possible to do this without splitting A?
>
> Oh, sure. Using the "replicate data rather than loop" principle, we
> stretch B to be the same shape as A, then compare. Try this:
>
> nCoords=25
> a=indgen(2,nCoords)
> b=[4,5]
> print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0
>
> (result is 1, there is a match)
>
> b=[4,6]
> print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0
>
> (result is 0, there is no match)
>
> To find *which* one(s) it matches, look at the inner part: Total(a EQ
> (Rebin(b, 2, nCoords)), 1) EQ 2
>
> This will be 1 where 'a' matches a pair of 'b' entries, use Where to
> find which one (or more) it matches.

A faster way to do this, if you don't care about the locations where
equality occurs, is to use array_equal(), which halts as soon as it find
an equal value:

print, array_equal(a[0,*] ne b[0] OR a[1,*] ne b[1],1b) eq 0b

JD
Re: Array comparison part 2 [message #32385 is a reply to message #32380] Fri, 04 October 2002 10:21 Go to previous message
tam is currently offline  tam
Messages: 48
Registered: February 2000
Member
Dick Jackson wrote:
> "Tom McGlynn" <tam@lheapop.gsfc.nasa.gov> wrote in message
> news:3D9D9E8B.8060401@lheapop.gsfc.nasa.gov...
>
>>
>> Sean Raffuse wrote:
>>
>>> Thanks for all the help on my first question. I now have a related
>>> question.
>>>
>>> What is the best (read: fastest) way to do the following:
>>>
>>> I have an array of coordinates, A = intarr(2,25)
>>> and I have another array of a specific location, B = [125,1043]
>>>
>>> I would like to determine if location B is one of the coordinates in
>>
> A. I
>
>>> need to know if A[*,?] = 125, 1043
>>>
>>> Is it possible to do this without splitting A?
>>
>> Just try
>>
>> w = where(b[0] eq a[0,*] and b[1] eq a[1,*])
>>
>> No need for anything complex here.
>> Or perhaps it's too early in the morning and I'm missing something...
>
>
> Of course, I was just solving the puzzle as given "without splitting A".
> Yours is certainly a better solution overall.
>

I guess I just assumed that "without splitting A" meant
without divvying things up in some kind of for loop.
Perhaps the original poster could be more explicit...
Always good to know the rules of the game!

Tom McGlynn
Re: Array comparison part 2 [message #32390 is a reply to message #32385] Fri, 04 October 2002 08:46 Go to previous message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
"Tom McGlynn" <tam@lheapop.gsfc.nasa.gov> wrote in message
news:3D9D9E8B.8060401@lheapop.gsfc.nasa.gov...
>
>
> Sean Raffuse wrote:
>> Thanks for all the help on my first question. I now have a related
>> question.
>>
>> What is the best (read: fastest) way to do the following:
>>
>> I have an array of coordinates, A = intarr(2,25)
>> and I have another array of a specific location, B = [125,1043]
>>
>> I would like to determine if location B is one of the coordinates in
A. I
>> need to know if A[*,?] = 125, 1043
>>
>> Is it possible to do this without splitting A?
>
> Just try
>
> w = where(b[0] eq a[0,*] and b[1] eq a[1,*])
>
> No need for anything complex here.
> Or perhaps it's too early in the morning and I'm missing something...

Of course, I was just solving the puzzle as given "without splitting A".
Yours is certainly a better solution overall.

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
Re: Array comparison part 2 [message #32393 is a reply to message #32390] Fri, 04 October 2002 07:20 Go to previous message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Chia Chang wrote:
>
> "Dick Jackson" <dick@d-jackson.com> wrote in message news:<oj_m9.473388$f05.21183164@news1.calgary.shaw.ca>...
...
> I like the 'where' function, below is how I would
> implement it to solve your particular problem:
>
> ------------------start test.pro-------------------
> pro test
>
> time0 = systime(1)
>
> a = indgen(2,25)
> b = [4,5]
>
> ;########################################
> ; PERFORM COMPARISON
> ;########################################
> xloc = where (a(0,*) EQ b[0])
> yloc = where (a(1,*) EQ b[1])
>
> ;########################################
> ; RETURN RESULTS
> ;########################################
> if xloc EQ yloc then begin
> print, 2, xloc
> endif else begin
> print, "No Match"
> endelse

Unless I'm missing something, that doesn't handle correctly the
possibility that there might be multiple matches, much less the
possibility that there might be a different set of matches for xloc than
for yloc. Of course, that possibility doesn't arise in your test data,
but it doesn't seem to be ruled out by the original problem description.
Re: Array comparison part 2 [message #32395 is a reply to message #32393] Fri, 04 October 2002 06:58 Go to previous message
tam is currently offline  tam
Messages: 48
Registered: February 2000
Member
Sean Raffuse wrote:
> Thanks for all the help on my first question. I now have a related
> question.
>
> What is the best (read: fastest) way to do the following:
>
> I have an array of coordinates, A = intarr(2,25)
> and I have another array of a specific location, B = [125,1043]
>
> I would like to determine if location B is one of the coordinates in A. I
> need to know if A[*,?] = 125, 1043
>
> Is it possible to do this without splitting A?
>
> Thanks,
>
> Sean
>
>

Just try

w = where(b[0] eq a[0,*] and b[1] eq a[1,*])

No need for anything complex here.
Or perhaps it's too early in the morning and I'm missing something...

Regards,
Tom McGlynn
Re: Array comparison part 2 [message #32398 is a reply to message #32395] Thu, 03 October 2002 17:27 Go to previous message
chia is currently offline  chia
Messages: 4
Registered: February 2002
Junior Member
"Dick Jackson" <dick@d-jackson.com> wrote in message news:<oj_m9.473388$f05.21183164@news1.calgary.shaw.ca>...
> "Sean Raffuse" <sean@me.wustl.edu> wrote in message
> news:anhkqb$ipf$1@newsreader.wustl.edu...
>> Thanks for all the help on my first question. I now have a related
>> question.
>>
>> What is the best (read: fastest) way to do the following:
>>
>> I have an array of coordinates, A = intarr(2,25)
>> and I have another array of a specific location, B = [125,1043]
>>
>> I would like to determine if location B is one of the coordinates in
> A. I
>> need to know if A[*,?] = 125, 1043
>>
>> Is it possible to do this without splitting A?
>
> Oh, sure. Using the "replicate data rather than loop" principle, we
> stretch B to be the same shape as A, then compare. Try this:
>
> nCoords=25
> a=indgen(2,nCoords)
> b=[4,5]
> print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0
>
> (result is 1, there is a match)
>
> b=[4,6]
> print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0
>
> (result is 0, there is no match)
>
> To find *which* one(s) it matches, look at the inner part:
> Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2
>
> This will be 1 where 'a' matches a pair of 'b' entries, use Where to
> find which one (or more) it matches.
>
> Cheers,

I like the 'where' function, below is how I would
implement it to solve your particular problem:

------------------start test.pro-------------------
pro test

time0 = systime(1)

a = indgen(2,25)
b = [4,5]

;########################################
; PERFORM COMPARISON
;########################################
xloc = where (a(0,*) EQ b[0])
yloc = where (a(1,*) EQ b[1])

;########################################
; RETURN RESULTS
;########################################
if xloc EQ yloc then begin
print, 2, xloc
endif else begin
print, "No Match"
endelse

print, 'Execution Time = ', systime(1)-time0, ' secs'

end
-----------------end test.pro-------------------
Re: Array comparison part 2 [message #32408 is a reply to message #32398] Thu, 03 October 2002 09:33 Go to previous message
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
"Sean Raffuse" <sean@me.wustl.edu> wrote in message
news:anhkqb$ipf$1@newsreader.wustl.edu...
> Thanks for all the help on my first question. I now have a related
> question.
>
> What is the best (read: fastest) way to do the following:
>
> I have an array of coordinates, A = intarr(2,25)
> and I have another array of a specific location, B = [125,1043]
>
> I would like to determine if location B is one of the coordinates in
A. I
> need to know if A[*,?] = 125, 1043
>
> Is it possible to do this without splitting A?

Oh, sure. Using the "replicate data rather than loop" principle, we
stretch B to be the same shape as A, then compare. Try this:

nCoords=25
a=indgen(2,nCoords)
b=[4,5]
print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0

(result is 1, there is a match)

b=[4,6]
print,Total(Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2) GT 0

(result is 0, there is no match)

To find *which* one(s) it matches, look at the inner part:
Total(a EQ (Rebin(b, 2, nCoords)), 1) EQ 2

This will be 1 where 'a' matches a pair of 'b' entries, use Where to
find which one (or more) it matches.

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL curve fitting program (correction)
Next Topic: Array Subscripting Memory Usage (watch out!)

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

Current Time: Wed Oct 08 20:01:23 PDT 2025

Total time taken to generate the page: 0.08094 seconds