Reputation: 39
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
Upvotes: 0
Views: 36
Reputation: 1810
Assuming combinations
does something similar to utils::combn
, you can use
matrix1 <- combn(10, 3)
matrix2 <- combn(20, 1)
pasted1 <- apply(matrix1,2,paste0,collapse=",")
pasted2 <- apply(matrix2,2,paste0,collapse=",")
expanded <- apply(expand.grid(pasted1,pasted2),1,paste0,collapse=",")
res <- lapply(strsplit(expanded,","),as.numeric)
Upvotes: 1
Reputation: 101618
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])))
)
)
Upvotes: 2