首页 > 解决方案 > finding combinations of elements from two matrices

问题描述

I would like a matrix of all the combinations of

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

i.e. we are choosing 3 numbers without repetition from a bag with numbers 1 to 10, then one number from 1 to 20

标签: rcombinations

解决方案


Are you looking for something like this?

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])))
  )
)

推荐阅读