我试图为给定的H生成以下方程的所有解.
H = 4时:
1) ALL solutions for x_1 + x_2 + x_3 + x_4 =4
2) ALL solutions for x_1 + x_2 + x_3 = 4
3) ALL solutions for x_1 + x_2 = 4
4) ALL solutions for x_1 =4
Run Code Online (Sandbox Code Playgroud)
对于我的问题,总有4个方程要解决(独立于其他方程).总共有2 ^(H-1)个解决方案.对于前一个,这里是解决方案:
1) 1 1 1 1
2) 1 1 2 and 1 2 1 and 2 1 1
3) 1 3 and 3 1 and 2 2
4) 4
Run Code Online (Sandbox Code Playgroud)
这是一个解决问题的R算法.
library(gtools)
H<-4
solutions<-NULL
for(i in seq(H))
{ …Run Code Online (Sandbox Code Playgroud) 我有两个这样的向量:
f1=c('a','b','c','d')
e1=c('e','f','g')
Run Code Online (Sandbox Code Playgroud)
它们有4 ^ 3个不同的排列。我需要在R软件中创建它们的所有可能排列。
(1):
a e
a f
a g
(2):
a e
a f
b g
...
Run Code Online (Sandbox Code Playgroud)
此外,我的真实数据非常庞大,我需要速度代码。
我试图做的是在给定特定样本大小的情况下生成1和0的所有可能排列。例如,对于一个n = 8的样本,我希望m = 2 ^ 8 = 256个可能的排列,即:
我已经在R中编写了一个函数来执行此操作,但是在n = 11之后,需要很长时间才能运行。我更喜欢R中的解决方案,但是如果它是另一种编程语言,则可能可以解决。谢谢!
PermBinary <- function(n){
n.perms <- 2^n
array <- matrix(0,nrow=n,ncol=n.perms)
# array <- big.matrix(n, n.perms, type='integer', init=-5)
for(i in 1:n){
div.length <- ncol(array)/(2^i)
div.num <- ncol(array)/div.length
end <- 0
while(end!=ncol(array)){
end <- end +1
start <- end + div.length
end <- start + div.length -1
array[i,start:end] <- 1
}
}
return(array)
}
Run Code Online (Sandbox Code Playgroud)