# How to generate all possible ways of dividing up the numbers from 1 to 46 into three groups?

2 views (last 30 days)
Ehsan Modiri on 20 Jan 2020
Commented: Ehsan Modiri on 20 Jan 2020
I want to generate all possible ways of dividing up the numbers from 1 to 46 into three groups with at least five numbers in each group. So far, I use the way below:
in1=46;
in2=5;
C1 = nchoosek(1:in1,in2); % Binomial coefficient or all combinations
C1 = [C1,zeros(size(C1,1),in1-in2)]; % Add some zeros to have a full matrix
x = 1:in1; % Number of values
for p = 1:size(C1,1)
t = true(1,in1); % Generate a logical matrix
t(C1(p,1:in2)) = false(1,in2); % Inserd zeros logical values in the pth row
C1(p,in2+1:in1) = x(t); % Insert remaining values into 6:46 indices
end
C2 = nchoosek(1:in1-in2,5); % Binomial coefficient or all combinations
C2 = [C2,zeros(size(C2,1),in1-in2-5)]; % Add some zeros to have a full matrix
x = 1:in1-in2;
for q = 1:size(C2,1)
t = true(1,in1-in2);
t(C2(q,1:in2)) = false(1,in2);
C2(q,in2+1:in1-in2) = x(t);
end
% A = zeros(size(C1,1)*size(C2,1),in1); % Generate a zeros matrix
tic
k = 0;
for p = 1:size(C1,1)
x = C1(p,1:in2);
y = C1(p,in2+1:in1);
for q = 1:size(C2,1)
k = k + 1;
A(k,:) = [x,y(C2(q,:))];
end
end
clear C1 C2 k p q t x y
toc
It takes long time, is there any other faster way to achieve this goal?

Image Analyst on 20 Jan 2020
You cannot do that. This would be the command:
combinations = perms(1:46);
and then you could generate 3 indexes, at least 5 apart, to extract the 3 groups
But the theoretical number of combinations would be 46 factorial, 5.50262215981209e+57, and that is way too big for your computer to handle.
Ehsan Modiri on 20 Jan 2020
I see, and due to this fact I ask this question.