可能的组合-顺序很重要 [英] Possible combinations - order is important
问题描述
我想创建一个表,其中包含使用Matlab在k个集合中的N个数字(按重要重要的顺序)的所有可能组合.
I want to create a table which contains all possible combinations, order is important, of N numbers in sets of k using matlab.
我尝试了Combinations = combntns(set,subset)
和Combinations = perms(v)
和Combinations = combnk(v,k)
,但是按照这些顺序并不重要.
I tried Combinations = combntns(set,subset)
and Combinations = perms(v)
and Combinations = combnk(v,k)
but in those order is not important.
一个例子:
nchoosek(1:5,3)
nchoosek(1:5,3)
ans =
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
同时还应包括
1 3 2
1 4 2
1 5 2
1 3 5
1 5 3
...
可能的组合数量由以下函数给出:
The number of possible combinations is given by the following by the function:
N!/(N-k)!
有没有可能使用matlab函数来做到这一点?
Is there a possible way to do it this using matlab functions?
推荐答案
尝试使用这种内存有效的解决方案:
Try this memory efficient solution:
n = 5; k = 3;
nk = nchoosek(1:n,k);
p=zeros(0,k);
for i=1:size(nk,1),
pi = perms(nk(i,:));
p = unique([p; pi],'rows');
end
p
应该包含您所描述的内容.在此示例中,至少为size(p,1) == factorial(n)/factorial(n-k)
或60
.
p
should contain what you are describing. At least size(p,1) == factorial(n)/factorial(n-k)
or 60
for this example.
这篇关于可能的组合-顺序很重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!