How to generate non repeat integer?

1 回表示 (過去 30 日間)
Rouyu Chen
Rouyu Chen 2023 年 4 月 7 日
コメント済み: Rouyu Chen 2023 年 4 月 7 日
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!

採用された回答

chicken vector
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
  1 件のコメント
Rouyu Chen
Rouyu Chen 2023 年 4 月 7 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by