how to make a code for distributing n different things among r groups such that each group get atleast certain amount?
1 回表示 (過去 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?
0 件のコメント
回答 (1 件)
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
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!