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

Home » Public Forums » archive » Re: Avoiding a for cicle
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: Avoiding a for cicle [message #19633 is a reply to message #19627] Tue, 11 April 2000 00:00 Go to previous messageGo to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
Benno Puetz <puetz@mpipsykl.mpg.de> writes:
> "J.D. Smith" wrote:
>> Ricardo Fonseca wrote:
>>>
>>> Hi
>>>
>>> I'm looking for a more efficient way of implementing the following (i.e.
>>> avoiding the for cycle) which is a routine for finding local maximuns
>>>
...
>>> for i = 1, nx-1 do $
>>> if ((Data[i] gt Data[i-1]) and (Data[i] gt Data[i+1])) then $
>>> max_pos = [[max_pos],i]
>>>
>>> ; and then throw away the first element...
>>
>> max_pos = where(data gt median(data,3))
>>
>
> While this is rather efficient concerning code length,
>
>
> maxpos = WHERE(TEMPORARY(data[0:nx-3]) LT TEMPORARY(data[1:nx-2]) AND $
>
> TEMPORARY(data[1:nx-2]) GT TEMPORARY(data[2:nx-1])) + 1
>
> should execute faster, especially for longer arrays

And the code-shortened version of this is:

maxpos = where((data LT data(1:*)) AND (data(1:*) GT data(2:*))) + 1


There are two key things to note here. First, TEMPORARY is not needed
when you are indexing an array, since subscripted array expressions
are already considered temporary. Second, IDL automatically truncates
1-D arrays in binary operations. So, the finite difference expression
normally written like this:

diff = data(1:nx-1) - data(0:nx-2)

can be written like this:

diff = data(1:*) - data

The two data arrays are of different length, so IDL takes the shortest
of each. Saves some keystrokes, and the calculation of NX.

Craig


--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Work at RSI!
Next Topic: Re: control precision for the axis ticks

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

Current Time: Sat Oct 11 02:06:30 PDT 2025

Total time taken to generate the page: 1.12104 seconds