2
$\begingroup$

Suppose I have a list of data which is oscillating. I want to calculate the positon of multiple local minimas present in the data. How can i do this?

$\endgroup$
0

2 Answers 2

2
$\begingroup$

You can use FindPeaks as follows:

ClearAll[findValleys]
findValleys = {1, -1} # & /@ FindPeaks[-#, 0] &;

SeedRandom[77]
l = RandomReal[{0, 1}, 10];

findValleys @ l
{{1, 0.000468887}, {3, 0.190391}, {6, 0.406117}, {9, 0.0674747}}
ListLinePlot[{l, findValleys @ l}, Joined -> {True, False}, 
 PlotStyle -> {Blue, Directive[Red, PointSize[Large]]}]

enter image description here

Alternatively, you can use MinDetect:

ClearAll[findValleys2]
findValleys2 = With[{p = PositionIndex[MinDetect @ #] @ 1}, Transpose[{p, #[[p]]}]] &;

findValleys @ l == findValleys2 @ l
 True
$\endgroup$
1
  • $\begingroup$ Thanks a lot, it is superhelpful $\endgroup$ Aug 26, 2020 at 10:52
3
$\begingroup$

You can define a simple function that compares neighbors of each points and checks if the value of a point is less than those of its neighbors.

positionOfMinima[l_] := Position[l, #] & /@ Pick[l[[2 ;; -2]], #, True] &@MovingMap[
   #[[1]] > #[[2]] && #[[2]] < #[[3]] &, l, 2
   ] // Flatten

l = RandomReal[{0, 1}, 10];
ListLinePlot[l, 
 Epilog -> {Red, PointSize[Large], 
   Point[{#, l[[#]]}] & /@ positionOfMinima[l]}]

Plot of random data with minima marked by red points

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.