Message Boards Message Boards

GROUPS:

Find the positions of the local minima and local maxima in a list?

Posted 1 month ago
229 Views
|
4 Replies
|
5 Total Likes
|

How do I find the positions of the elements in a list that are smaller (or greater) than both the previous and the next element in that list. So for instance, suppose I have the list data={2,4,1,3,4,5,3,2,1,5}. In this list the third and nineth element are smallerr than the elements right before and right after them. They are the local minima in the list. How do I find their positions? I suppose with Position[data,....&]. But what criteria should I use in this expression? The same question goes for the local maxima.

4 Replies

Partition into triples. Example:

ll = RandomInteger[{1, 10}, 30]

(* Out[1]= {3, 3, 8, 3, 5, 1, 1, 10, 9, 1, 8, 1, 7, 10, 2, 7, 4, 5, 6, \
4, 4, 1, 5, 1, 5, 9, 5, 4, 5, 5} *)

Get local maxima:

Flatten@
  Position[Partition[ll, 3, 1], {a_, b_, c_} /; b > a && b > c] + 1

(* Out[4]= {3, 5, 8, 11, 14, 16, 19, 23, 26} *)

I think this what you are looking for

data = {2, 4, 1, 3, 4, 5, 3, 2, 1, 5}
d1 = Partition[data, 3, 1]
d2 = Select[d1, #[[1]] > #[[2]] < #[[3]] &]
pp = Position[d1, #] & /@ d2
p1=1 + Flatten[pp]

Here is one more :)

data = {2, 4, 1, 3, 4, 5, 3, 2, 1, 5};
Last /@ SequencePosition[Sign@Differences[data], {-1, 1}]

Thank you Daniel, Hans and Henrik. Your methods work nicely.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract