Re: Array comparison part 2 [message #32398 is a reply to message #32395] |
Thu, 03 October 2002 17:27   |
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-------------------
|
|
|