How to generate non repeat integer?

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

chicken vector
chicken vector il 7 Apr 2023
Modificato: chicken vector il 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

Più risposte (0)

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by