How to generate all possible ways of dividing up the numbers from 1 to 46 into three groups?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (1)
Image Analyst
el 20 de En. de 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.
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!