matlab 学生分组程序

抽签程序
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

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值