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

Home » Public Forums » archive » Re: Little help on arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Little help on arrays [message #38043 is a reply to message #38040] Mon, 16 February 2004 11:23 Go to previous messageGo to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Mon, 16 Feb 2004 09:27:52 -0600, Craig Markwardt wrote:

>
> Nuno Oliveira <nmoliveira@fc.ul.pt> writes:
>
>
>> How do I compare one array with another? I want to avoid comparing
>> position per position.
>>
>> IDL> if [1,1] eq [1,1] then print, 'bingo!'
>> % Expression must be a scalar or 1 element array in this context: <BYTE
>> Array[2]>.
>> % Execution halted at: $MAIN$
>
> The other posters have good ideas. My own idiom for this comparison
> is:
>
> if total(abs(X-Y)) EQ 0 then print, 'bingo!'

If you have a recent enough IDL, ARRAY_EQUAL() is the way to go,
because it stops the comparison as soon as it determines the arrays
are not equal, and the second array need not be an array, but can be a
scalar. I use this all the time for tricks like:

if ~array_equal(array ge 0,1b)

which efficiently determines if any element of array is not ge 0
(i.e. is negative). I also think it's a little more clear what the
intention is than TOTAL. Both the TOTAL and ARRAY_EQUAL method are
much faster than using WHERE, since they don't have to construct list
of indices. Craig's construct is very good when comparing floats, up
to some tolerance:

if total(abs(X-Y) ge 1.e-6) eq 0 then print, 'Close enough!'

but of course this can also be expressed as:

if array_equal(abs(X-Y) lt 1.e-6,1b) then print,'Close enough!'

which would run somewhat faster.

JD
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Little help on arrays - 2nd try
Next Topic: array multiplying (for a change)

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

Current Time: Thu Oct 09 16:23:38 PDT 2025

Total time taken to generate the page: 0.88219 seconds