Finding local maxima and minima

前端 未结 14 1669
说谎
说谎 2020-11-22 07:24

I\'m looking for a computationally efficient way to find local maxima/minima for a large list of numbers in R. Hopefully without for loops...

For exampl

14条回答
  •  一生所求
    2020-11-22 08:02

    diff(diff(x)) (or diff(x,differences=2): thanks to @ZheyuanLi) essentially computes the discrete analogue of the second derivative, so should be negative at local maxima. The +1 below takes care of the fact that the result of diff is shorter than the input vector.

    edit: added @Tommy's correction for cases where delta-x is not 1...

    tt <- c(1,2,3,2,1, 1, 2, 1)
    which(diff(sign(diff(tt)))==-2)+1
    

    My suggestion above ( http://statweb.stanford.edu/~tibs/PPC/Rdist/ ) is intended for the case where the data are noisier.

提交回复
热议问题