抽签程序
clear all;
nameListId = 1:37;%总人数
tempListId = nameListId;
groupNum = 1;%预组个数,若groupNum=1,则相当于打乱排序
groupNumAdd = 0;
groupAdd = [];
groupMembers = round(max(nameListId)/groupNum);%组成员个数
group = zeros(groupNum,round(max(nameListId)/groupNum));
%排组预处理
%若总人数除以组数有余数
num = length(nameListId);
%1:余数大等于于组员数一半
if mod(length(nameListId),groupNum)>(groupMembers/2)
groupAdd = zeros(1,mod(length(nameListId),groupNum));
%则随机抽取多出的人自成一组
for ii = 1: mod(length(nameListId),groupNum)
id = round(1+(num-1)*rand(1));
while min(abs(groupAdd-repmat(tempListId(id),[1,length(groupAdd)])))==0
id = round(1+(num-1)*rand(1));
end
groupAdd(ii) = tempListId(id);
tempListId(id) = [];
num = length(tempListId);
end
groupNumAdd = 1;
group = zeros(groupNum,round((length(nameListId)-length(groupAdd))/groupNum));
for ii = 1:groupNum
num = length(tempListId);
for jj = 1: (length(nameListId)-length(groupAdd))/groupNum
id = round(1+(num-1)*rand(1));
while min(abs(group(ii,:)-repmat(tempListId(id),[1,length(group(ii,:))])))==0
id = round(1+(num-1)*rand(1));
end
group(ii,jj) = tempListId(id);
tempListId(id) = [];
num = length(tempListId);
end
end
groupNum = groupNum+groupNumAdd;
groupC = cell(2,1);
groupC{1,1} = group;
groupC{2,1} = groupAdd;
else
for ii = 1:groupNum
num = length(tempListId);
for jj = 1: groupMembers
id = round(1+(num-1)*rand(1));
while min(abs(group(ii,:)-repmat(tempListId(id),[1,length(group(ii,:))])))==0
id = round(1+(num-1)*rand(1));
end
group(ii,jj) = tempListId(id);
tempListId(id) = [];
num = length(tempListId);
end
end
if ~isempty(tempListId)%2:余数小于等于组员数一半%则多出的人随机均匀加到其他组中
groupNumList = 1:groupNum;
tempGroupListNum = groupNumList;
groupAddId = zeros(length(tempListId),2);
num = length(tempListId);
for ii = 1:num
iDAdd = round(1+(length(tempGroupListNum)-1)*rand(1));%要加入的组号
while min(abs(groupAddId(ii,1)-repmat(tempGroupListNum(iDAdd),[1,1])))==0
iDAdd = round(1+(length(tempGroupListNum)-1)*rand(1));
end
groupAddId(ii,:) = [iDAdd,tempListId(1)];
tempGroupListNum(iDAdd) = [];
tempListId(1) = [];
end
groupC = cell(2,1);
groupC{1,1} = group;
groupC{2,1} = groupAddId;
end
end
matlab 学生分组程序
于 2018-05-04 09:35:55 首次发布