If I have interpreted your request correctly, you can do what you ask with two calls on 'nchoosek', one to choose three numbers out of nine and the other to choose three numbers out of six, (plus some additional processing of course.) What I have understood is that you want to generate all possible ways of dividing up the numbers from 1 to 9 into three groups with three numbers in each group. The total number of these is 9!/3!/3!/3! = 1680.
C1 = nchoosek(1:9,3);
C1 = [C1,zeros(size(C1,1),6)];
x = 1:9;
for p = 1:size(C1,1)
t = true(1,9); t(C1(p,1:3)) = false(1,3);
C1(p,4:9) = x(t);
end
C2 = nchoosek(1:6,3);
C2 = [C2,zeros(size(C2,1),3)];
x = 1:6;
for q = 1:size(C2,1)
t = true(1,6); t(C2(q,1:3)) = false(1,3);
C2(q,4:6) = x(t);
end
A = zeros(size(C1,1)*size(C2,1),9);
k = 0;
for p = 1:size(C1,1)
x = C1(p,1:3);
y = C1(p,4:9);
for q = 1:size(C2,1)
k = k + 1;
A(k,:) = [x,y(C2(q,:))];
end
end
The matrix A which has nine columns will contain your desired three groups: A(:,1:3), A(:,4:6), and A(:,7:9).
Best Answer