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

Home » Public Forums » archive » Re: Select Equal elements from 2 arrays
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: Select Equal elements from 2 arrays [message #60952] Mon, 23 June 2008 09:46
ianpaul.freeley is currently offline  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 Go to previous message
matthias.demuzere is currently offline  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 #60954 is a reply to message #60953] Mon, 23 June 2008 08:45 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Ryan. writes:

> You can read about Union and Intersections on David Fanning's Website
> here: http://www.dfanning.com/tips/set_operations.html

And here you can read about the famous "Where function gotcha":

http://www.dfanning.com/misc_tips/noidea.html

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Select Equal elements from 2 arrays [message #60955 is a reply to message #60954] Mon, 23 June 2008 08:32 Go to previous message
Ryan. is currently offline  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 Go to previous message
humanumbrella is currently offline  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 Go to previous message
matthias.demuzere is currently offline  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 Go to previous message
humanumbrella is currently offline  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 #60959 is a reply to message #60958] Mon, 23 June 2008 08:28 Go to previous message
matthias.demuzere is currently offline  matthias.demuzere
Messages: 32
Registered: January 2006
Member
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?
Re: Select Equal elements from 2 arrays [message #60960 is a reply to message #60959] Mon, 23 June 2008 08:27 Go to previous message
matthias.demuzere is currently offline  matthias.demuzere
Messages: 32
Registered: January 2006
Member
Hmmm...when I try this, get the following error:

% Attempt to subscript A with <LONG ( -1)> is out of
range.

Is this because my arrays have different lengths, or just because they
are in a different format?
Re: Select Equal elements from 2 arrays [message #60961 is a reply to message #60960] Mon, 23 June 2008 08:17 Go to previous message
humanumbrella is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Select Equal elements from 2 arrays
Next Topic: IDL_IDLbridge, status

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

Current Time: Wed Oct 08 15:37:28 PDT 2025

Total time taken to generate the page: 0.00729 seconds