从两个矩阵中找到元素的组合

时间:2021-06-01 13:08:52

标签: r combinations

我想要一个包含所有组合的矩阵

matrix1 <- combinations(10,3)
matrix2 <- combinations(20,1)

即我们从数字 1 到 10 的袋子中选择 3 个不重复的数字,然后从 1 到 20 中选择一个数字

2 个答案:

答案 0 :(得分:2)

你在寻找这样的东西吗?

matrix1 <- combn(10, 3)
matrix2 <- combn(20, 1)

res <- c(
  outer(
    1:ncol(matrix1),
    1:ncol(matrix2),
    Vectorize(function(x, y) list(c(matrix1[, x], matrix2[, y])))
  )
)

答案 1 :(得分:1)

假设 cons-stream 执行类似于 combinations 的操作,您可以使用

utils::combn