求局部最大值和极小值我正在寻找一种计算效率高的方法来为R中的大量数字寻找局部极大值/极小值,希望没有for循环.。例如,如果我有一个数据文件,如1 2 3 2 1 1 2 1,我希望函数返回3和7,这是局部最大值的位置。
                    
                    
                3 回答
                            杨__羊羊
                            
                                
                            
                        
                        
                                                
                    TA贡献1943条经验 获得超7个赞
x <- c(1, 2, 3, 2, 1, 1, 2, 1)library(zoo) xz <- as.zoo(x) rollapply(xz, 3, function(x) which.min(x)==2)# 2 3 4 5 6 7 #FALSE FALSE FALSE TRUE FALSE FALSE rollapply(xz, 3, function(x) which.max(x)==2)# 2 3 4 5 6 7 #FALSE TRUE FALSE FALSE FALSE TRUE
which.minwhich.max.
rxz <- rollapply(xz, 3, function(x) which.max(x)==2) index(rxz)[coredata(rxz)]#[1] 3 7
- 3 回答
 - 0 关注
 - 627 浏览
 
添加回答
举报
0/150
	提交
		取消
	