寻找局部最大值和最小值 [英] Finding local maxima and minima

查看:79
本文介绍了寻找局部最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种计算效率高的方法来为 R 中的大量数字找到局部最大值/最小值.希望没有 for 循环......

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...

例如,如果我有一个像 1 2 3 2 1 1 2 1 这样的数据文件,我希望函数返回 3 和 7,它们是局部最大值的位置.

For example, if I have a datafile like 1 2 3 2 1 1 2 1, I want the function to return 3 and 7, which are the positions of the local maxima.

推荐答案

diff(diff(x)) (or diff(x,differences=2): 谢谢到@ZheyuanLi)本质上计算二阶导数的离散模拟,所以在局部最大值处应该是负的.下面的 +1 处理了 diff 的结果比输入向量短这一事实.

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.

编辑:为 delta-x 不是 1 的情况添加了@Tommy 的更正...

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

我上面的建议( http://statweb.stanford.edu/~tibs/PPC/Rdist/ ) 适用于数据噪声较大的情况.

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

这篇关于寻找局部最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆