How to generate non repeat integer?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Rouyu Chen
el 7 de Abr. de 2023
Comentada: Rouyu Chen
el 7 de Abr. de 2023
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 comentarios
Respuesta aceptada
chicken vector
el 7 de Abr. de 2023
Editada: chicken vector
el 7 de Abr. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Random Number Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!