作业帮 > 综合 > 作业

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

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/20 10:25:33
在1到20中求13个数的所有组合,不是求组合数,是求出所有的组合,希望有matlab代码
在1到20中求13个数的所有组合,不是求组合数,是求出所有的组合,希望有matlab代码
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