在1到20中求13个数的所有组合,不是求组合数,是求出所有的组合,希望有matlab代码

2024-12-29 22:23:43
推荐回答(2个)
回答1:

In matlab, you can use a function to obtain the combinations: nchoosek()

For example:

>> nchoosek(20,13) % get the total number of choosing 13 from 20, which is 77520

ans =

77520

>> y = nchoosek(1:20,13); % get all combinations of choosing 13 from 20
>> size(y)

ans =

77520 13

the list of y is too long to fit here, let's see the first few rows:

>> y(1:5,:)

ans =

1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 14
1 2 3 4 5 6 7 8 9 10 11 12 15
1 2 3 4 5 6 7 8 9 10 11 12 16
1 2 3 4 5 6 7 8 9 10 11 12 17

Let me give you another full example, such as choosing 3 from 6:

>> nchoosek(1:6,3)

ans =

1 2 3
1 2 4
1 2 5
1 2 6
1 3 4
1 3 5
1 3 6
1 4 5
1 4 6
1 5 6
2 3 4
2 3 5
2 3 6
2 4 5
2 4 6
2 5 6
3 4 5
3 4 6
3 5 6
4 5 6

回答2:

是个排列组合中的组合问题。
所有的组合一共有20*19*18*...*8=4.8272e+014种,太多了,用matlab排列也是for,循环次数也非常庞大,写好程序也难以实现。