【发布时间】:2015-04-11 04:52:02
【问题描述】:
我正在尝试制作一个由 1 到 100(整数)之间的 5 个数字的所有组合组成的矩阵,总和为 100。如果我可以为每 5 个数字设置最小值和最大值,那将会更大。 我做的最简单的方法是做 5 个嵌套循环。
for (a in min:max )
{
for (b in min:max )
{
for (c in min:max)
{
for(d in min:max)
{
for (e in min:max)
{
for (f in min:max)
{
for (g in min:max)
{
for (h in min:max)
{
port <- c (a,b,c,d,e,f,g,h)
if(a+b+c+d+e+f+g+h==100) {portif <- rbind(port,portif)}
}}}}}}}}
但我很确定在 R 中有比这些漂亮的慢循环更好的方法。
编辑: - 是的,顺序很重要
如果我可以为每个 a,b,c 设置不同的最小值和最大值 ...
非常感谢您的帮助
【问题讨论】:
-
数字的顺序重要吗?你允许重复的数字吗? (1,1,1,1,96) 和 (1,1,96,1,1) 一样吗?
标签: r loops combinations