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

Home » Public Forums » archive » Re: Array comparison
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: Array comparison [message #32330] Wed, 02 October 2002 13:44
mchinand is currently offline  mchinand
Messages: 66
Registered: September 1996
Member
In article <Pine.GHP.4.32.0210021447020.18221-100000@icesat2.gsfc.nasa.gov>,
Jeff Guerber <jeff.guerber@gsfc.nasa.gov> wrote:
> On Wed, 2 Oct 2002, Mike Chinander wrote:
>
>> How about:
>>
>> if n_elements(uniq(([a,b])(sort([a,b])))) ne n_elements([a,b]) then print,
>> 'arrays have matching values'
>
> Uhh, doesn't that claim a match if one of the arrays contains two (or
> more) identical elements, even if the arrays have no elements in common?
>
> IDL> a=[1,2,3,4,5]
> IDL> b=[6,6,7]
> IDL> if n_elements(uniq(([a,b])(sort([a,b])))) ne n_elements([a,b]) then
> print,'match'
> match
> IDL>
>
> Jeff Guerber
>
Yeah, I realized this after reading the code from David Fanning's site
that he linked to in this thread. Applying UNIQ to the two arrays prior
to concatenating them should get around this.

--Mike


--
Michael Chinander
m-chinander@uchicago.edu
Department of Radiology
University of Chicago
Re: Array comparison [message #32334 is a reply to message #32330] Wed, 02 October 2002 12:04 Go to previous message
Jeff Guerber is currently offline  Jeff Guerber
Messages: 41
Registered: July 2000
Member
On Wed, 2 Oct 2002, Mike Chinander wrote:

> How about:
>
> if n_elements(uniq(([a,b])(sort([a,b])))) ne n_elements([a,b]) then print,
> 'arrays have matching values'

Uhh, doesn't that claim a match if one of the arrays contains two (or
more) identical elements, even if the arrays have no elements in common?

IDL> a=[1,2,3,4,5]
IDL> b=[6,6,7]
IDL> if n_elements(uniq(([a,b])(sort([a,b])))) ne n_elements([a,b]) then
print,'match'
match
IDL>

Jeff Guerber
Re: Array comparison [message #32341 is a reply to message #32334] Wed, 02 October 2002 08:12 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
"Sean Raffuse" <sean@me.wustl.edu> writes:

>>
>> I believe that the original poster wanted was the values, and not the
>> indices.
>>
>
> That is indeed true. Actually, I'm not interested in anything but whether
> or not an intersection exists. Neither the values nor the indices are
> important.
>
> I am not doing this on very large arrays. However, I may be doing it many,
> many times.

Another way to do set comparisons is with HISTOGRAM. This method is
used within the set_operations.pro file that David Fanning just
mentioned. It is also used by CMSET_OP, if the dynamic range of
values is small. If the dynamic range is large, then there is a
penalty because a huge histogram is created to store a few values.
CMSET_OP chooses dynamically whether to use the HISTOGRAM or
SORT/UNIQ.

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Array comparison [message #32342 is a reply to message #32341] Wed, 02 October 2002 08:09 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Craig Markwardt wrote:
> Reimar Bauer <R.Bauer@fz-juelich.de> writes:
>
>
>> Sean Raffuse wrote:
>>
>>> Hello,
>>>
>>> I would like to compare two arrays of different size. What I want to know
>>> is if the two arrays share ANY of the same values, regardless of index.
>>>
>>> e.g.
>>>
>>> Array1 = [1,2,3,4,5]
>>> Array2 = [5,6,7]
>>>
>>> Comparing these two arrays should return true because they both have the
>>> value 5.
>>>
>>> I know I could do this in a loop, but I need the speed and I'm sure IDL can
>>> do this in one line. I'm just not sure how.
>>
> [ ... ]
>
>> I am using which_indices
>
>
>
> Greetings Reimar--
>
> I believe that the original poster wanted was the values, and not the
> indices.


Ok, it looks only similiar to another FAQ Question so I did not read the
whole mail. My mistake. Sorry

best wishes

Reimar



--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: Array comparison [message #32343 is a reply to message #32342] Wed, 02 October 2002 07:53 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Sean Raffuse (sean@me.wustl.edu) writes:

> That is indeed true. Actually, I'm not interested in anything but whether
> or not an intersection exists. Neither the values nor the indices are
> important.
>
> I am not doing this on very large arrays. However, I may be doing it many,
> many times.

I've always found the set intersection, union, and difference
routines on my web page useful for these kinds of operations.
I've modified them some to do better error checking, etc.,
but this only takes a couple of seconds.

http://www.dfanning.com/tips/set_operations.html

Cheers,

David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: Array comparison [message #32344 is a reply to message #32343] Wed, 02 October 2002 07:34 Go to previous message
Sean Raffuse is currently offline  Sean Raffuse
Messages: 46
Registered: July 2001
Member
>
> I believe that the original poster wanted was the values, and not the
> indices.
>

That is indeed true. Actually, I'm not interested in anything but whether
or not an intersection exists. Neither the values nor the indices are
important.

I am not doing this on very large arrays. However, I may be doing it many,
many times.

Thanks,

Sean
Re: Array comparison [message #32348 is a reply to message #32344] Wed, 02 October 2002 06:19 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
Reimar Bauer <R.Bauer@fz-juelich.de> writes:

> Sean Raffuse wrote:
>> Hello,
>>
>> I would like to compare two arrays of different size. What I want to know
>> is if the two arrays share ANY of the same values, regardless of index.
>>
>> e.g.
>>
>> Array1 = [1,2,3,4,5]
>> Array2 = [5,6,7]
>>
>> Comparing these two arrays should return true because they both have the
>> value 5.
>>
>> I know I could do this in a loop, but I need the speed and I'm sure IDL can
>> do this in one line. I'm just not sure how.
[ ... ]
>
> I am using which_indices


Greetings Reimar--

I believe that the original poster wanted was the values, and not the
indices.

Here is what WHICH_INDICES uses:

> FOR i=0L,n_clients DO BEGIN
> a=WHERE(master EQ client[i],count_a)
> IF count_a GT 0 THEN build_vector,result,a
> ENDFOR

I am not sure that the FOR loop with an interior WHERE() will scale to
very large arrays, or very fast calculations, as the original poster
appeared to be requesting.

This actually ties into JD's point from the other day. A loop of
WHERE()'s can be quite slow, even slower than a loop of basic
operations. Its speed here is roughly proportional to N*M, where N
and M are the two input vector sizes. The SORT/UNIQ method is
proportional to (N+M)*ALOG(N+M), which can be a big savings for large
arrays.

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Array comparison [message #32351 is a reply to message #32348] Wed, 02 October 2002 01:11 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Sean Raffuse wrote:
> Hello,
>
> I would like to compare two arrays of different size. What I want to know
> is if the two arrays share ANY of the same values, regardless of index.
>
> e.g.
>
> Array1 = [1,2,3,4,5]
> Array2 = [5,6,7]
>
> Comparing these two arrays should return true because they both have the
> value 5.
>
> I know I could do this in a loop, but I need the speed and I'm sure IDL can
> do this in one line. I'm just not sure how.
>
> Thanks,
>
> Sean Raffuse
>
>

I am using which_indices


http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/which_indices.tar.gz
or as idl5.5 binary
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/which_indices.sav


ix=which_indices(Array1,Array2)
print,array1[ix]
5

regards

Reimar

--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: Array comparison [message #32352 is a reply to message #32351] Tue, 01 October 2002 19:59 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
"Sean Raffuse" <sean@me.wustl.edu> writes:

> I would like to compare two arrays of different size. What I want to know
> is if the two arrays share ANY of the same values, regardless of index.
>
> e.g.
>
> Array1 = [1,2,3,4,5]
> Array2 = [5,6,7]
>
> Comparing these two arrays should return true because they both have the
> value 5.

Mike has a nice one-off comparison you can use. For your kinds of
set-comparison tests, I use CMSET_OP with an intersection test.

intersection = cmset_op(array1, 'AND', array2, count=ct)
if ct GT 0 then print, 'Arrays match'

The variable INTERSECTION contains the intersection between the two
sets. The internals of CMSET_OP are ultimately the same as Mike's
approach, but with a bit more flexibility.

Craig

http://cow.physics.wisc.edu/~craigm/idl/idl.html

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Array comparison [message #32356 is a reply to message #32352] Tue, 01 October 2002 17:07 Go to previous message
mchinand is currently offline  mchinand
Messages: 66
Registered: September 1996
Member
In article <and6i0$8l4$1@newsreader.wustl.edu>,
Sean Raffuse <sean@me.wustl.edu> wrote:
> Hello,
>
> I would like to compare two arrays of different size. What I want to know
> is if the two arrays share ANY of the same values, regardless of index.
>
> e.g.
>
> Array1 = [1,2,3,4,5]
> Array2 = [5,6,7]
>
> Comparing these two arrays should return true because they both have the
> value 5.
>
> I know I could do this in a loop, but I need the speed and I'm sure IDL can
> do this in one line. I'm just not sure how.
>
> Thanks,
>
> Sean Raffuse
>
>

How about:

if n_elements(uniq(([a,b])(sort([a,b])))) ne n_elements([a,b]) then print,
'arrays have matching values'


--Mike


--
Michael Chinander
m-chinander@uchicago.edu
Department of Radiology
University of Chicago
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Looking for more ideas on code ...
Next Topic: Re: ROI application...

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

Current Time: Wed Oct 08 14:56:29 PDT 2025

Total time taken to generate the page: 0.00527 seconds