当前位置: 动力学知识库 > 问答 > 编程问答 >

matlab - Possible combinations - order is important

问题描述:

I want to create a table which contains all possible combinations, order is important, of N numbers in sets of k using matlab.

I tried Combinations = combntns(set,subset) and Combinations = perms(v) and Combinations = combnk(v,k)but in those order is not important.

An example:

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

While it should also include

 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)!

source: Mathisfun.com

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 should contain what you are describing. At least size(p,1) == factorial(n)/factorial(n-k) or 60 for this example.

分享给朋友:
您可能感兴趣的文章:
随机阅读: