给出一个向量和一个和,例如极限= c(1,2,5,6,7,6)和 总和= 10。 给定的约束是:
x1 <=限制[1]
x2 <=限制[2]
x3 <=限制[3]
x4 <=限制[4]
x5 <=限制[5]
x6 <=限制[6]
x1 + x2 + x3 + x4 + x5 + x6 =总和
我想找到一个向量:c(x1,x2,x3,x4,x5,x6)满足这些约束。我是这样写的:
get_vector <-function(sum, limits){
res_vec <-c()
left <- sum
for (i in seq(1:(length(limits)-1))){
res_vec<-c(res_vec, sample(c(0:min(limits[i],left)), 1))
left <- left - res_vec[i]
}
res_vec[length(limits)] <- left
}
return (res_vec)
}
,但它似乎有时会失败(我已经检查过了)。 有人可以给出更准确的建议来解决这个问题吗?
谢谢
答案 0 :(得分:1)
Writing NAMESPACE
答案 1 :(得分:0)
这是一种获取所有可能解决方案的方法。
library(partitions)
allPartitions <- compositions(10, 6, include.zero = FALSE)
limits <- c(1, 2, 5, 6, 7, 6)
good <- apply(allPartitions, 2, function(x) all(x <= limits))
results <- allPartitions[, good]
> t(results)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 4 1 1 1
[2,] 1 1 5 1 1 1
[3,] 1 2 3 2 1 1
[4,] 1 1 4 2 1 1
[5,] 1 2 2 3 1 1
[6,] 1 1 3 3 1 1
[7,] 1 2 1 4 1 1
[8,] 1 1 2 4 1 1
[9,] 1 1 1 5 1 1
[10,] 1 2 3 1 2 1
[11,] 1 1 4 1 2 1
[12,] 1 2 2 2 2 1
[13,] 1 1 3 2 2 1
[14,] 1 2 1 3 2 1
[15,] 1 1 2 3 2 1
[16,] 1 1 1 4 2 1
[17,] 1 2 2 1 3 1
[18,] 1 1 3 1 3 1
[19,] 1 2 1 2 3 1
[20,] 1 1 2 2 3 1
[21,] 1 1 1 3 3 1
[22,] 1 2 1 1 4 1
[23,] 1 1 2 1 4 1
[24,] 1 1 1 2 4 1
[25,] 1 1 1 1 5 1
[26,] 1 2 3 1 1 2
[27,] 1 1 4 1 1 2
[28,] 1 2 2 2 1 2
[29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