Sunday 15 August 2010

Matlab generate all possible team combinations -



Matlab generate all possible team combinations -

there's lots of questions similar mine haven't found quite i'm looking yet. i'm working on project optimize teaming in class, , not sure how generate possible team combinations.

say have vector that's list of numbered people,

<1,2,3,4,5....,n>

i want generate possible combinations of teams k people per team, k smaller n. output should matrices rows teams. each matrix have k columns, , n/k rows (corresponding number of teams).

for example, vector is<1,2,3,4>. want combinations of teams of 2. possible output matrices [1,2;3,4],[1,3;2,4], , [1,4;2,3]. i'd know how scale n , k value.

i have done incomplete testing, seems work.

code:

%// data: n = 6; %// number of people k = 2; %// team size. assumed split p %// let's go: m = unique(perms(ceil((1:n)/k)), 'rows').'; %'// transpose convenience result = nan(n/k, k, size(m,2)); %// preallocate t = 1:n/k [ii, ~] = find(m==t); result(t,:,:) = reshape(ii, k, []); end result = result(:,:,all(diff(result(:,1,:))>0, 1));

the result matrices given result(:,:,1), result(:,:,2) etc.

explanation:

the key steps are:

line m = unique(perms(ceil((1:n)/k)), 'rows').': assigns k different team numbers, 1 each grouping of n/k people, , creates different permutations of numbers. includes possible team groupings.

for loop: translates above representation matrix format want: each team described row containing n/k labels set {1,2,...,n}, telling people belong team. within each row, labels increasing.

line result = result(:,:,all(diff(result(:,1,:))>0, 1)): removes matrices row-permutations of others. keeping matrices first column increasing.

examples:

for n=4; k=2,

>> result result(:,:,1) = 1 2 3 4 result(:,:,2) = 1 3 2 4 result(:,:,3) = 1 4 2 3

for n=6; k=2,

>> result result(:,:,1) = 1 2 3 4 5 6 result(:,:,2) = 1 2 3 5 4 6 result(:,:,3) = 1 2 3 6 4 5 result(:,:,4) = 1 3 2 4 5 6 ...

matlab combinations discrete-mathematics

No comments:

Post a Comment