How to generate non repeat integer?
1 次查看(过去 30 天)
显示 更早的评论
Dear experts,
I have randomly generated values 1,2,3 to allocate 15 elements into three groups, I then want to generate random ranking in each group, i.e. generate three groups of consecutive numbers, and then randonly assign them to each group member. I was wondering what code can help me achieve this?
Many thanks!
0 个评论
采纳的回答
chicken vector
2023-4-7
编辑:chicken vector
2023-4-7
% Input data:
numberOfElements = 15;
numberOfGroups = 3;
% Pre-process:
elementsPerGroup = numberOfElements / numberOfGroups;
ranking = zeros(numberOfGroups, elementsPerGroup);
% Loop over groups:
for group = 1 : numberOfGroups
% Randomise ranking:
ranking(group,:) = randperm(elementsPerGroup);
end
You can use structures to have clear outputs:
% Input data:
numberOfElements = 15;
numberOfGroups = 3;
% Pre-process:
elementsPerGroup = numberOfElements / numberOfGroups;
ranking = struct;
% Loop over groups:
for group = 1 : numberOfGroups
% Randomise ranking:
ranking.(['group' num2str(group)]) = randperm(elementsPerGroup);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!