[英]all distinct combinations of N=50 and K=3
How do I create a matrix in R with 50 individuals having labels K=1,2,3 and then all possible outcomes?如何在 R 中创建一个矩阵,其中 50 个人的标签为 K=1,2,3,然后是所有可能的结果? It does not seem to be either combinations or per muations.
它似乎既不是组合也不是排列。 So as an example with N=2 the following, only then not 2 rows but 50. So I get a 50x3^50 matrix.
因此,作为以下 N=2 的示例,只有 50 行不是 2 行。所以我得到一个 50x3^50 矩阵。
> cbind(c(1,1),c(1,2),c(1,3),c(2,1),c(2,2),c(2,3),c(3,1),c(3,2),c(3,3))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 1 1 2 2 2 3 3 3
[2,] 1 2 3 1 2 3 1 2 3
And for N=3:对于 N=3:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27]
[1,] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3
[2,] 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3
[3,] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
This gets you partway there but I think there might be a practical problem with your definition.这让你走到了那里,但我认为你的定义可能存在实际问题。 What it sounds like you want is impractical for N=50.
对于 N = 50,您想要的听起来是不切实际的。
f <- function(n, K) {
(replicate(n, 1:K, simplify = FALSE) ## n copies of 1:K
|> do.call(what = expand.grid) ## pass these all to expand.grid
|> as.matrix() ## convert to matrix
|> t() ## transpose
)
}
f(2)
and f(3)
look like your examples above. f(2)
和f(3)
看起来像你上面的例子。 However ...然而 ...
f(2)
is 2 x 9 (ie, 2 by K^2) f(2)
是 2 x 9(即 2 乘 K^2)f(3)
is 3 x 27 (3 by K^3) f(3)
是 3 x 27(3 乘 K^3)f(4)
is 4 x 81 (3 by K^4) f(4)
是 4 x 81(3 乘 K^4) ... so if we continue on this way, the results for N=50 will not be 50 x 50^2 but N x K^N = 50 x 3^50, which is 3.5 x 10^25 ... ...所以如果我们继续这样下去,N=50 的结果将不是 50 x 50^2,而是 N x K^N = 50 x 3^50,即 3.5 x 10^25 ...
... so you might need to rethink something about your strategy. ...因此您可能需要重新考虑您的策略。 (50^3 would be OK, but 3^50 is not!)
(50^3 可以,但 3^50 不行!)
You can try the code below for code-golfing if you like如果您愿意,可以尝试下面的代码进行代码打高尔夫球
k <- 3
n <- 4
t(expand.grid(rep(list(1:k), n)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.