My original function is so complex. There is a complex computation. So, I have a condition where my function stop if this condition is met. That is good for me to not go further for the unneeded computation. Due to this, I try to give a simple example here to make my question clear for you. The main idea of my original function is to do some computation, and if the given condition is met, then the function stop otherwise it completes for the second computation.
我原来的功能太复杂了。有一个复杂的计算。所以,我有一个条件,如果满足这个条件,我的函数会停止。这对我来说不好进行不必要的计算。因此,我试着在这里给出一个简单的例子,让我的问题清楚。我的原始函数的主要思想是进行一些计算,如果满足给定条件,则函数停止,否则它完成第二次计算。
Hence, I write a simple function, where the loop
of this function is based on a specific condition. In my function I try to do the following:
因此,我编写了一个简单的函数,其中此函数的循环基于特定条件。在我的函数中,我尝试执行以下操作:
- Compute the first two multiplication.
- If the value of the second multiplication is smaller than the first one, then, I would like the function to stop and return me the result.
- Else, I would like my function to compute the third result. And compare it with the second one. If the third result is smaller than the second one then stop, and so on for all my element.
计算前两个乘法。
如果第二次乘法的值小于第一次乘法,那么,我希望函数停止并返回结果。
否则,我希望我的函数计算第三个结果。并将其与第二个进行比较。如果第三个结果小于第二个结果然后停止,依此类推我的所有元素。
Here is my try:
这是我的尝试:
myfun <- function(x,y){
xy <- list()
n <- length(x)
for (i in 1:n){
xy[[1]] <- x[[1]]*y[[1]]
xy[[2]] <- x[[2]]*y[[2]]
if(xy[[2]] < xy[[1]]){
stop
}else{
xy[[i]] <- x[[i]]*y[[i]]
}
}
return(xy)
}
x <- rnorm(10, 0,1)
y <- rnorm(10, 0, 1)
myres <- myfun(x, y)
The problem of my function is that the condition is only worked with the first and second element. I would like to do the 3
listed steps.
我的功能问题是条件只适用于第一个和第二个元素。我想做3个列出的步骤。
Any help, please?
有什么帮助吗?
1 个解决方案
#1
2
It will be slow and painful to try to write out all of these conditions. You can create the vector of products, and evaluate whether its difference is positive and negative in one go, and then return the product if the condition holds (as suggested in comments):
尝试写出所有这些条件将是缓慢而痛苦的。您可以创建产品向量,并一次评估其差异是正面还是负面,然后在条件成立时返回产品(如评论中所示):
func <- function(x, y) {
xy <- x*y
d.xy <- diff(xy)
if (all(d.xy > 0)) {
xy
} else {
cat("Product", which(d.xy < 0)[1]+1, "is greater than product",
which(d.xy < 0)[1])
}
}
set.seed(8)
x <- rnorm(10, 0,1)
y <- rnorm(10, 0, 1)
func(x, y)
#> Product 3 is greater than product 2
x <- 1:10
y <- 2:11
func(x, y)
#> [1] 2 6 12 20 30 42 56 72 90 110
Additions due to question edits
OK in light of your edits to your question, here is a version that assumes that the cost of computing the function of x and y is very time consuming:
好的,根据您对问题的编辑,这是一个假设计算x和y函数的成本非常耗时的版本:
## assuming time to compute vector of results is v. v. high
func2 <- function(x, y) {
cval <- x[1] * y[1]
res <- cval
for (i in 2:length(x)) {
res[i] <- x[i] * y[i]
if (res[i] < cval) {
break
}
cval <- res[i]
}
res
}
set.seed(8)
x <- rnorm(10, 0,1)
y <- rnorm(10, 0, 1)
func2(x, y)
#> [1] 0.06426797 0.24543874 -0.19531099
x <- 1:10
y <- 2:11
func2(x, y)
#> [1] 2 6 12 20 30 42 56 72 90 110
In this version of the function I return the values of the result that are computed (rather than returning nothing), as if it takes a long time to compute them might be better to hang on to them in case they prove useful.
在这个函数版本中,我返回计算结果的值(而不是不返回任何内容),好像花费很长时间来计算它们可能会更好地挂起它们以防它们证明有用。
#1
2
It will be slow and painful to try to write out all of these conditions. You can create the vector of products, and evaluate whether its difference is positive and negative in one go, and then return the product if the condition holds (as suggested in comments):
尝试写出所有这些条件将是缓慢而痛苦的。您可以创建产品向量,并一次评估其差异是正面还是负面,然后在条件成立时返回产品(如评论中所示):
func <- function(x, y) {
xy <- x*y
d.xy <- diff(xy)
if (all(d.xy > 0)) {
xy
} else {
cat("Product", which(d.xy < 0)[1]+1, "is greater than product",
which(d.xy < 0)[1])
}
}
set.seed(8)
x <- rnorm(10, 0,1)
y <- rnorm(10, 0, 1)
func(x, y)
#> Product 3 is greater than product 2
x <- 1:10
y <- 2:11
func(x, y)
#> [1] 2 6 12 20 30 42 56 72 90 110
Additions due to question edits
OK in light of your edits to your question, here is a version that assumes that the cost of computing the function of x and y is very time consuming:
好的,根据您对问题的编辑,这是一个假设计算x和y函数的成本非常耗时的版本:
## assuming time to compute vector of results is v. v. high
func2 <- function(x, y) {
cval <- x[1] * y[1]
res <- cval
for (i in 2:length(x)) {
res[i] <- x[i] * y[i]
if (res[i] < cval) {
break
}
cval <- res[i]
}
res
}
set.seed(8)
x <- rnorm(10, 0,1)
y <- rnorm(10, 0, 1)
func2(x, y)
#> [1] 0.06426797 0.24543874 -0.19531099
x <- 1:10
y <- 2:11
func2(x, y)
#> [1] 2 6 12 20 30 42 56 72 90 110
In this version of the function I return the values of the result that are computed (rather than returning nothing), as if it takes a long time to compute them might be better to hang on to them in case they prove useful.
在这个函数版本中,我返回计算结果的值(而不是不返回任何内容),好像花费很长时间来计算它们可能会更好地挂起它们以防它们证明有用。