how to make a code for distributing n different things among r groups such that each group get atleast certain amount?

조회 수: 2 (최근 30일)
if I have 7 machines and I want to distribute them in 3 groups with all possible combinations such that each group has atleast 2 machines.how can I code this?

답변 (1개)

Guillaume
Guillaume 2017년 1월 24일
편집: Guillaume 2017년 1월 24일
machines = 1:7;
groupcount = 3;
minmachinepergroup = 2;
minmachines = minmachinepergroup * groupcount;
assert(numel(machines) >= minmachines, 'Not enough to fill the minimum in each goup');
rmachines = machines(randperm(numel(machines))); %randomise the machines
groupdest = [repmat(1:groupcount, 1, minmachinepergroup), randi(groupcount, 1, numel(machines) - minmachines)]; %distribute first the minimum per group, then randomise
groups = accumarray(groupdest(:), rmachines, [], @(v) {v}); %distribute the machines into the groups
  댓글 수: 1
summyia qamar
summyia qamar 2017년 1월 24일
how can I change this?
C = zeros(3^7,7);
t = 0;
for k = 0:(3^7)-1
s = dec2base(k,3,7);
if length(unique(s))==3
t = t+1;
C(t,:) = s-0+1;
end
end
C = C(1:t,:);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by