in reply to finding local minima/maxima for a discrete array
so ...
Finding the indices of the top k maxima is left as a trivial exercise for the reader (hint: push a structure onto @maxima containing the value and index).my @values = qw(1 4 3 2 5 6 3 4 2); my @maxima = (); my $k = 2; foreach my $index (1 .. $#values - 1) { push @maxima, $values[$index] if( $values[$index-1] < $values[$index] && $values[$index+1] < $values[$index] ); } # @maxima now contains qw(4 6 4); print join(', ', (sort { $a <=> $b } @maxima)[0 .. $k - 1])."\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: finding local minima/maxima for a discrete array
by ysth (Canon) on Aug 01, 2007 at 07:25 UTC |