在R中每个区间中找到函数最大值的最快方法(Fastest way to find maximum value of a function in each interval in R)

编程入门 行业动态 更新时间:2024-10-28 07:30:02
在R中每个区间中找到函数最大值的最快方法(Fastest way to find maximum value of a function in each interval in R)

我有一个矢量, v和一个间隔矢量, w 。 我想在每个区间中找到函数的最大值f(x) 。 有没有比跟踪代码更快的方法来查找结果? 例如:

v = c(3.5, 2.5, 4, 6.5, 10, 2.3, 1.8, 4.7, 12, 11.5) w = c(0, 5, 15, 20) f = function(x){x^2} > max = unlist(list(sapply(split(v, cut(v, w),drop = TRUE), function(v) v[which.max(f(v))])), use.names = FALSE) > max [1] 4.7 12.0

I have a vector, v, and a vector of intervals, w. I want to find the maximum value of a function, f(x), in each interval. Is there a faster way than following code for finding the result? For example:

v = c(3.5, 2.5, 4, 6.5, 10, 2.3, 1.8, 4.7, 12, 11.5) w = c(0, 5, 15, 20) f = function(x){x^2} > max = unlist(list(sapply(split(v, cut(v, w),drop = TRUE), function(v) v[which.max(f(v))])), use.names = FALSE) > max [1] 4.7 12.0

最满意答案

那么findInterval和tapply 。 findInterval就像cut ,但没有转换为因子的开销

tapply(v,findInterval(v,w),function(x)x[which.max(f(x))]) # 1 2 # 4.7 12.0

或者如果你想要最大值

tapply(f(v),findInterval(v,w),max) # 1 2 # 22.09 144.00

或者你可以使用这样一个事实,即你的函数对于所有正值都是单调递增的。

f(tapply(v,findInterval(v,w),max))

请注意,您需要指定边界处发生的情况(阅读帮助文件)

library(microbenchmark) microbenchmark( mnel = tapply(v,findInterval(v,w),max), flodel = unname(vapply(split(f(v), cut(v, w), drop = TRUE), max, numeric(1L))), flodel2 = unname(vapply(split(seq_along(v), findInterval(v, w)), function(i, v, fv)v[i][which.max(fv[i])], numeric(1L), v, f(v)))) # Unit: microseconds # expr min lq median uq max neval # mnel 260.945 262.9155 264.2265 276.0645 458.670 100 # flodel 331.218 334.3585 336.0580 351.1985 694.715 100 #flodel2 124.998 127.3230 128.5170 137.0505 354.545 100

What about findInterval and tapply. findInterval is like cut, but without the overhead of converting to factors

tapply(v,findInterval(v,w),function(x)x[which.max(f(x))]) # 1 2 # 4.7 12.0

Or if you want the maximum value

tapply(f(v),findInterval(v,w),max) # 1 2 # 22.09 144.00

Or you could use the fact that your function is monotonically increasing for all positive values and do.

f(tapply(v,findInterval(v,w),max))

Note that you will need to specify what happens at the boundaries (read the help file)

library(microbenchmark) microbenchmark( mnel = tapply(v,findInterval(v,w),max), flodel = unname(vapply(split(f(v), cut(v, w), drop = TRUE), max, numeric(1L))), flodel2 = unname(vapply(split(seq_along(v), findInterval(v, w)), function(i, v, fv)v[i][which.max(fv[i])], numeric(1L), v, f(v)))) # Unit: microseconds # expr min lq median uq max neval # mnel 260.945 262.9155 264.2265 276.0645 458.670 100 # flodel 331.218 334.3585 336.0580 351.1985 694.715 100 #flodel2 124.998 127.3230 128.5170 137.0505 354.545 100

更多推荐

本文发布于:2023-07-05 01:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   区间   函数   最快   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!