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

Re: Avoiding a for cicle



"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
>
> ; 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))
 

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

-- 
Benno Puetz
Kernspintomographie
Max-Planck-Institut f. Psychiatrie            Tel.: +49-89-30622-413
Kraepelinstr. 10                              Fax : +49-89-30622-520
80804 Muenchen, Germany