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

*Subject*: Re: Avoiding a for cicle*From*: "J.D. Smith" <jdsmith(at)astro.cornell.edu>*Date*: Tue, 11 Apr 2000 11:41:10 -0400*Newsgroups*: comp.lang.idl-pvwave*Organization*: Cornell University*References*: <B51278FB.4C3D%zamb@physics.ucla.edu> <38EE2E2F.D5849FB3@astro.cornell.edu> <38F3210B.F2A82B04@mpipsykl.mpg.de> <onu2h8k7mx.fsf@cow.physics.wisc.edu>*Sender*: verified_for_usenet(at)cornell.edu (jts11 on vodka.tn.cornell.edu)*Xref*: news.doit.wisc.edu comp.lang.idl-pvwave:19207

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 |*|

**Follow-Ups**:**Re: Avoiding a for cicle***From:*J.D. Smith

**References**:**Avoiding a for cicle***From:*Ricardo Fonseca

**Re: Avoiding a for cicle***From:*J.D. Smith

**Re: Avoiding a for cicle***From:*Benno Puetz

**Re: Avoiding a for cicle***From:*Craig Markwardt

- Prev by Date:
**Re: Work at RSI!** - Next by Date:
**Re: control precision for the axis ticks** - Prev by thread:
**Re: Avoiding a for cicle** - Next by thread:
**Re: Avoiding a for cicle** - Index(es):