r - 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
解决方案
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])))
)
)
推荐阅读
- laravel - 如何在cpanel上发送验证邮件的工作队列?
- c++ - 为什么 QVariant 将 char 类型视为整数类型?
- javascript - 获取另一个对象中具有与查询匹配的属性值的对象的所有键
- godot - 我可以使用 godots OS.execute 执行 Godot 包中的文件吗?
- sql-server - 通过存储过程在表中添加两个动态列,这些列必须有基于条件的数据
- powershell - 如何在文件夹中逐个选择文件以使用 PowerShell 重命名扩展名?
- intellij-idea - 在顶部显示整个 Sass 嵌套路径/级别?
- angular - 如何在 ag-grid 中选择细节网格
- arrays - How-come sort 将遍历数组中的所有元素,其中函数仅比较 2 个元素?
- gdal - GDAL geometry Within() 与 Contains() 函数有什么区别?