[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Avoiding a for cicle



Craig Markwardt wrote:
> 
> 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.
>

Alright code slingers... new challenge... find location of all peaks in a region
of n points (n odd), monotonically decreasing away from the peak.  I.e. find
peaks of width n.


e.g. an n=5 peak:

  -
 - -
-   -

I'll reserve my entry until I see the contenders.  Points are awarded for
unusual use of obscure IDL functions, brevity, style, lip synch, and
congeniality.

JD

-- 
 J.D. Smith                             |*|      WORK: (607) 255-5842    
 Cornell University Dept. of Astronomy  |*|            (607) 255-6263
 304 Space Sciences Bldg.               |*|       FAX: (607) 255-5875 
 Ithaca, NY 14853                       |*|