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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Avoiding a for cicle [message #19627] Tue, 11 April 2000 00:00
John-David T. Smith is currently offline  John-David T. Smith
Messages: 384
Registered: January 2000
Senior Member
David Fanning wrote:
>
> J.D. Smith (jdsmith@astro.cornell.edu) writes:
>
>> 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.
>
> This contest is rigged. I don't see any possible way I
> can win without points also being awarded for humor and
> general naivet�. :-(
>

Any humor embedded within IDL statements will be accepted. An example might be
the use of widget_info() or routine_names() for this calculation. Naivet� falls
under the congeniality category, also known as the not-yet-a-grumpy-old-IDL-hack
category.

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 |*|
Re: Avoiding a for cicle [message #19629 is a reply to message #19627] Tue, 11 April 2000 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
J.D. Smith (jdsmith@astro.cornell.edu) writes:

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

This contest is rigged. I don't see any possible way I
can win without points also being awarded for humor and
general naivet�. :-(

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: Avoiding a for cicle [message #19631 is a reply to message #19627] Tue, 11 April 2000 00:00 Go to previous message
John-David T. Smith is currently offline  John-David T. Smith
Messages: 384
Registered: January 2000
Senior Member
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 |*|
Re: Avoiding a for cicle [message #19633 is a reply to message #19627] Tue, 11 April 2000 00:00 Go 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
------------------------------------------------------------ --------------
Re: Avoiding a for cicle [message #19636 is a reply to message #19627] Tue, 11 April 2000 00:00 Go to previous message
Benno Puetz is currently offline  Benno Puetz
Messages: 16
Registered: March 2000
Junior Member
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
"J.D. Smith" wrote:
<blockquote TYPE=CITE>Ricardo Fonseca wrote:
<br>>
<br>> Hi
<br>>
<br>> I'm looking for a more efficient way of implementing the following
(i.e.
<br>> avoiding the for cycle) which is a routine for finding local maximuns
<br>>
<br>> ; Data is a 1D Array
<br>>
<br>> s = Size(Data)
<br>>
<br>> nx = s[1]
<br>>
<br>> max_pos = [-1]
<br>>
<br>> for i = 1, nx-1 do $
<br>>&nbsp;&nbsp;&nbsp; if ((Data[i] gt Data[i-1]) and (Data[i] gt Data[i+1]))
then $
<br>> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; max_pos = [[max_pos],i]
<br>>
<br>> ; and then throw away the first element...
<p>max_pos = where(data gt median(data,3))
<br>&nbsp;</blockquote>
While this is rather efficient concerning code length,
<br>&nbsp;
<pre>&nbsp;maxpos = WHERE(TEMPORARY(data[0:nx-3]) LT TEMPORARY(data[1:nx-2]) AND&nbsp; $</pre>

<pre> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; TEMPORARY(data[1:nx-2]) GT TEMPORARY(data[2:nx-1])) + 1</pre>

<pre></pre>
should execute faster, especially for longer arrays
<br>--&nbsp;<br>
Benno Puetz<br>
Kernspintomographie<br>
Max-Planck-Institut f. Psychiatrie&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;
Tel.: +49-89-30622-413<br>
Kraepelinstr. 10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Fax : +49-89-30622-520<br>
80804 Muenchen, Germany
<br>&nbsp;</html>
Re: Avoiding a for cicle [message #19659 is a reply to message #19627] Fri, 07 April 2000 00:00 Go to previous message
John-David T. Smith is currently offline  John-David T. Smith
Messages: 384
Registered: January 2000
Senior Member
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
>
> ; Data is a 1D Array
>
> s = Size(Data)
>
> nx = s[1]
>
> max_pos = [-1]
>
> 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))

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 |*|
Re: Avoiding a for cicle [message #19660 is a reply to message #19659] Fri, 07 April 2000 00:00 Go to previous message
landsman is currently offline  landsman
Messages: 93
Registered: August 1991
Member
In article <B51278FB.4C3D%zamb@physics.ucla.edu>,
Ricardo Fonseca <zamb@physics.ucla.edu> 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
>
> ; Data is a 1D Array
>
> s = Size(Data)
>
> nx = s[1]
>
> max_pos = [-1]
>
> 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...
>
> Any ideas? Thanks in advance, Ricardo
>

And JD Smith gave the solution:

> max_pos = where(data gt median(data,3))

Cool! I think this can be described as an almost literal example of
"thinking outside of the box"

Wayne Landsman landsman@mpb.gsfc.nasa.gov


Sent via Deja.com http://www.deja.com/
Before you buy.
Re: Avoiding a for cicle [message #19663 is a reply to message #19659] Thu, 06 April 2000 00:00 Go to previous message
Med Bennett is currently offline  Med Bennett
Messages: 109
Registered: April 1997
Senior Member
Hi Ricardo,

You can use the SHIFT command in this situation - do something like

max_pos = where(data gt shift(data,-1) and data gt shift(data,1))

of course you have to check if the edge effects make sense in your case.

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
>
> ; Data is a 1D Array
>
> s = Size(Data)
>
> nx = s[1]
>
> max_pos = [-1]
>
> 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...
>
> Any ideas? Thanks in advance, Ricardo
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Oct 08 13:52:27 PDT 2025

Total time taken to generate the page: 0.00743 seconds