Finding local maxima and minima

前端 未结 14 1899
说谎
说谎 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:19

    In the pracma package, use the

    tt <- c(1,2,3,2,1, 1, 2, 1)
    tt_peaks <- findpeaks(tt, zero = "0", peakpat = NULL,
           minpeakheight = -Inf, minpeakdistance = 1, threshold = 0, npeaks = 0, sortstr = FALSE)
    
      [,1] [,2] [,3] [,4]
      [1,]  3    3    1    5
      [2,]  2    7    6    8
    

    That returns a matrix with 4 columns. The first column is showing the local peaks' absolute values. The 2nd column are the indices The 3rd and 4th column are the start and end of the peaks (with potential overlap).

    See https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/findpeaks for details.

    One caveat: I used it in a series of non-integers, and the peak was one index too late (for all peaks) and I do not know why. So I had to manually remove "1" from my index vector (no big deal).

提交回复
热议问题