Re: Select Equal elements from 2 arrays [message #60952] |
Mon, 23 June 2008 09:46 |
ianpaul.freeley
Messages: 18 Registered: March 2007
|
Junior Member |
|
|
On Jun 23, 10:46 am, maffie <matthias.demuz...@geo.kuleuven.be> wrote:
> Yes...but the manual also states further on:
>
> c = 1 2 3 4 5 5 4 3 2 1
> d = 0 2 4
> Subscripts of c that equal d: 1
>
> Note that WHERE found only one element in the array d that equals an
> element in array c. This is because only the first three elements of c
> were searched, since d has only three elements.
>
> That is where my problem is...all the common elements are after the
> number of elements in the smallest array..So they are missed by the
> statement....I have tried many other things, also something like:
> common_indices = indices(Uniq(indices, indices(sort(indices)))
>
> But also this doesn't seem to work...
>
> Any other ideas or suggestions???
yes, just use the match function from the astronomy users library.
http://idlastro.gsfc.nasa.gov/
A = [0,8,16,60,36,49,60]
B =[1,8,33,36,49]
match, a,b,suba,subb
print, 'matching values =', a(suba)
print, 'should be the same values = ', b(subb)
|
|
|
Re: Select Equal elements from 2 arrays [message #60953 is a reply to message #60952] |
Mon, 23 June 2008 08:46  |
matthias.demuzere
Messages: 32 Registered: January 2006
|
Member |
|
|
Yes...but the manual also states further on:
c = 1 2 3 4 5 5 4 3 2 1
d = 0 2 4
Subscripts of c that equal d: 1
Note that WHERE found only one element in the array d that equals an
element in array c. This is because only the first three elements of c
were searched, since d has only three elements.
That is where my problem is...all the common elements are after the
number of elements in the smallest array..So they are missed by the
statement....I have tried many other things, also something like:
common_indices = indices(Uniq(indices, indices(sort(indices)))
But also this doesn't seem to work...
Any other ideas or suggestions???
|
|
|
|
Re: Select Equal elements from 2 arrays [message #60955 is a reply to message #60954] |
Mon, 23 June 2008 08:32  |
Ryan.
Messages: 77 Registered: March 2006
|
Member |
|
|
maffie wrote:
> Dear all,
>
> I have 2 arrays, of unequal length, who hold a number of indices that
> are referring to a location of another array.
> Now, I would like to choose these elements which appear in both arrays
> A and B.
>
> For example:
>
> A = [0,8,16,36,49]
> B =[1,8,33,36,49]
>
> result = [8,49]
>
> Isn't there a simple function to do that? I can't find anything
> although I have tried with SORT, UNIQ and WHERE??
>
> Thank you!
> Matthias
>
You can read about Union and Intersections on David Fanning's Website
here: http://www.dfanning.com/tips/set_operations.html
Or you can do this with CM_SETOP from Craig Markwardt's library here:
http://cow.physics.wisc.edu/~craigm/idl/idl.html
I've used Craig's CM_SETOP before because it can handle performing these
operations on strings.
Hope this helps,
Ryan.
|
|
|
Re: Select Equal elements from 2 arrays [message #60956 is a reply to message #60955] |
Mon, 23 June 2008 08:37  |
humanumbrella
Messages: 52 Registered: June 2008
|
Member |
|
|
On Jun 23, 11:32 am, maffie <matthias.demuz...@geo.kuleuven.be> wrote:
> Probably the command with the WHERE statement is not working because
> this only looks for the first elements of the shortest array. If the
> equal elements or after that location, they are missed, and a -1 is
> returned, not?
>
> This would be what is happening in my case, so I need something which
> can deal with the different array lengths (of 61 and 368 for
> example)....
>
> Thank you!
the manual says it can be use for different lengths
hmm...
; Now compare two arrays of different lengths:
c = [1,2,3,4,5,5,4,3,2,1]
d = [0,2,4]
PRINT, 'c = ', c
PRINT, 'd = ', d
result=WHERE(c EQ d)
PRINT, 'Subscripts of c that equal d: ', result
good luck,
--Justin
|
|
|
Re: Select Equal elements from 2 arrays [message #60957 is a reply to message #60955] |
Mon, 23 June 2008 08:32  |
matthias.demuzere
Messages: 32 Registered: January 2006
|
Member |
|
|
Probably the command with the WHERE statement is not working because
this only looks for the first elements of the shortest array. If the
equal elements or after that location, they are missed, and a -1 is
returned, not?
This would be what is happening in my case, so I need something which
can deal with the different array lengths (of 61 and 368 for
example)....
Thank you!
|
|
|
Re: Select Equal elements from 2 arrays [message #60958 is a reply to message #60955] |
Mon, 23 June 2008 08:30  |
humanumbrella
Messages: 52 Registered: June 2008
|
Member |
|
|
On Jun 23, 11:28 am, maffie <matthias.demuz...@geo.kuleuven.be> wrote:
> when I check that, it seems that both have the same format:
>
> IDL> help, /str, a
> A LONG = Array[61]
> IDL> help, /str, b
> B LONG = Array[368]
>
> Any idea what could be wrong here?
Result of -1 would indicate no matches.
where (a eq b) returns the index values where the two arrays are the
same.
it will be -1 if there are no matches.
Cheers,
--Justin
|
|
|
|
|
Re: Select Equal elements from 2 arrays [message #60961 is a reply to message #60960] |
Mon, 23 June 2008 08:17  |
humanumbrella
Messages: 52 Registered: June 2008
|
Member |
|
|
On Jun 23, 11:11 am, maffie <matthias.demuz...@geo.kuleuven.be> wrote:
> Dear all,
>
> I have 2 arrays, of unequal length, who hold a number of indices that
> are referring to a location of another array.
> Now, I would like to choose these elements which appear in both arrays
> A and B.
>
> For example:
>
> A = [0,8,16,36,49]
> B =[1,8,33,36,49]
>
> result = [8,49]
>
> Isn't there a simple function to do that? I can't find anything
> although I have tried with SORT, UNIQ and WHERE??
>
> Thank you!
> Matthias
A = [0,8,16,36,49]
B =[1,8,33,36,49]
print, a[where(a eq b)]
gives me: 8 36 49
Good luck (:
--Justin
|
|
|