Check if coordinate is in array [message #34072] |
Wed, 19 February 2003 19:34  |
Thomas Gutzler
Messages: 44 Registered: November 2002
|
Member |
|
|
Hi again.
Here is the daily riddle:
Is there a faster way to check, if a coordinate (2-element array) is
element of an array of coordinates (2,n-element array) than this:
FUNCTION is_element_of_2D, x, arr
xidx = WHERE(arr[0,*] EQ x[0], c1)
yidx = WHERE(arr[1,*] EQ x[1], c2)
IF (c1 + c2 GT 0) THEN tmp = WHERE(MATRIX_MULTIPLY(xidx, $
1./TRANSPOSE(yidx)) EQ 1, c3) ELSE c3 = 0
RETURN, c3 NE 0
END
Example:
arr = [[1,2], [3,4], [3,2]]
x = [3,4]
y = [2,3]
print, is_element_of_2D(x, arr)
print, is_element_of_2D(y, arr)
Tom
|
|
|
Re: Check if coordinate is in array [message #34192 is a reply to message #34072] |
Thu, 20 February 2003 14:30  |
condor
Messages: 35 Registered: January 2002
|
Member |
|
|
Thomas Gutzler <tgutzler@ee.uwa.edu.au> wrote in message news:<3E544CAB.6020006@ee.uwa.edu.au>...
> Hi again.
>
> Here is the daily riddle:
> Is there a faster way to check, if a coordinate (2-element array) is
> element of an array of coordinates (2,n-element array) than this:
>
> FUNCTION is_element_of_2D, x, arr
> xidx = WHERE(arr[0,*] EQ x[0], c1)
> yidx = WHERE(arr[1,*] EQ x[1], c2)
> IF (c1 + c2 GT 0) THEN tmp = WHERE(MATRIX_MULTIPLY(xidx, $
> 1./TRANSPOSE(yidx)) EQ 1, c3) ELSE c3 = 0
> RETURN, c3 NE 0
> END
>
> Example:
> arr = [[1,2], [3,4], [3,2]]
> x = [3,4]
> y = [2,3]
> print, is_element_of_2D(x, arr)
> print, is_element_of_2D(y, arr)
>
> Tom
I don't know the context of this snippet of code, but in my math
background there is the strong notion that all plane geometry should
always be done in complex numbers as you get the whole world of
Wirtinger calculus for free.
In IDL you get this:
IDL> help,f,g
F COMPLEX = ( 1.00000, 2.00000)
G COMPLEX = Array[4]
IDL> print,g,format='(10(2i2,3x))'
3 4 8 3 1 2 4 5
IDL> print,where(g eq f)
2
So if you could keep your coordinates in complex variables from the
beginning, you can match them and compare them to each other and
such...
|
|
|
Re: Check if coordinate is in array [message #34217 is a reply to message #34072] |
Thu, 20 February 2003 06:36  |
Chris[1]
Messages: 23 Registered: January 2003
|
Junior Member |
|
|
"Thomas Gutzler" <tgutzler@ee.uwa.edu.au> wrote in message
news:3E544CAB.6020006@ee.uwa.edu.au...
> Hi again.
>
> Here is the daily riddle:
> Is there a faster way to check, if a coordinate (2-element array) is
> element of an array of coordinates (2,n-element array) than this:
>
> FUNCTION is_element_of_2D, x, arr
> xidx = WHERE(arr[0,*] EQ x[0], c1)
> yidx = WHERE(arr[1,*] EQ x[1], c2)
> IF (c1 + c2 GT 0) THEN tmp = WHERE(MATRIX_MULTIPLY(xidx, $
> 1./TRANSPOSE(yidx)) EQ 1, c3) ELSE c3 = 0
> RETURN, c3 NE 0
> END
>
Wouldn't it be faster to do the following:
;; initialize the return value
c2 = 0
;; find the matching first coordinates
xidx = where(arr[0,*] eq x[0],c1)
;;search only among coordinates where the first matches
if c1 gt 0 then yidx = where(arr[1,xidx] eq x[1],c2)
return, c2 gt 0
Note also that it is possible that an element of your yidx might be zero,
which is going to cause you some problems.
Chris
|
|
|