2

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?

Manos C
  • 333
  • 4
  • 16
  • 1
    The reason why you didn't find the solution before is that you were looking for combinations. Actually if the order matters it is referred to as permutations. – Dennis Jaheruddin Oct 18 '13 at 12:30

2 Answers2

4

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.

chappjc
  • 30,359
  • 6
  • 75
  • 132
0

try this, if k = 3:

combvec(1:N,1:N,1:N)'
NJS
  • 1