comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: Check if coordinate is in array
items: Re: Check if coordinate is in array

Check if coordinate is in array
type: item
description: 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
creator: Thomas Gutzler
date: 2003-02-20T03:34:03-00:00

Re: Check if coordinate is in array
type: item
description: "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
creator: Chris[1]
date: 2003-02-20T14:36:48-00:00

Re: Check if coordinate is in array
type: item
description: 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...
creator: condor
date: 2003-02-20T22:30:22-00:00