How to generate non repeat integer?

1 view (last 30 days)
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!

Accepted Answer

chicken vector
chicken vector on 7 Apr 2023
Edited: chicken vector on 7 Apr 2023
% 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

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!