如何在不重复的情况下在matlab中将20个名字的列表随机分配到4组

how to randomly assign a list of 20 names into 4 groups in matlab without repeating

我需要从列表中随机选择 select 个名字,然后将它们随机放入指定数量的组中。例如,我在一个列表中有 20 个名字,我希望我的代码随机选择一个名字并将它们放入 4 个组中的一个,直到列表的末尾。在代码的最后,我想输出每个生成的组或团队的名称。

这是我目前所拥有的,但是它没有用。

data = readtable("NameList (2).xlsx");

%Check and make sure the script is accurately pulling names
Names = (data{:,1}); disp(Names)

%Arranging and Organizing Data
number_of_people = numel(data);
%Scramble array
s= number_of_people(randperm(length(number_of_people)))
number_of_groups = 4; 
divisions = sort(randperm(number_of_people-1, number_of_groups-1) + 1, 'ascend')
divisions = [0, divisions, number_of_people] 

%Cell array that will hold the groups
groups = cell(1, number_of_groups);

 for i= 1:number_of_groups
    indexes = divisions(i)+1;                                                         
  usersInThisGroups = length(indexes);
  fprintf('Assigning %d participant (indexes %d to %d) to group %d.\n', ...
    usersInThisGroups, divisions(i)+1,divisions(i+1), i);
  groups{i} = s(indexes);
 end
 
celldisp(groups); % Display groups in command window.


如果希望每组的元素个数尽可能接近,建议先确定二维数组的大小。然后将所有人随机分配到二维数组中。我的实现如下:

data = readtable("NameList (2).xlsx");

%Check and make sure the script is accurately pulling names
Names = (data{:,1}); disp(Names)


number_of_people = numel(data);
number_of_groups = 4; 

%Initialize array
num_of_element_group = uint32(ceil(number_of_people/4))
groups = cell(number_of_groups, num_of_element_group);

%Initialize a random array
rand_num_arr = randperm(number_of_people)

%Using random array to assign people to group randomly
for i=0:number_of_people-1 %Iteration of the people list
    groups(mod(i,number_of_groups)+1, floor(i/number_of_groups)+1) = ... 
    Names(rand_num_arr(i+1)) %Assign the cell with randomly selected name    
end

celldisp(groups); % Display groups in command window.