random choice of elements for n times, without choosing the same element more than once
4 ビュー (過去 30 日間)
古いコメントを表示
Hello! I am trying to randomly choose one element from a string for a predifined number of times but I don't know how I can avoid getting the same element more than once.. Beneath is the code I wrote (my question may be more comprehensible through the comment in the code).
%% ************************************ *******************************************
letters = 'ABCD';
ntrials = 3;
nletters = 4;
for i = 2: ntrials
for j = 1: nletters
*% it needs to display a random letter from letters but the same
% letter must NOT appear more than once in this nested loop.*
end
end
% ******************************* end of code ************************************************
output example: BCAD ADCB
Thanks a lot!
0 件のコメント
採用された回答
その他の回答 (3 件)
Cam Salzberger
2017 年 10 月 12 日
Hello Maria,
There are typically two ways to do this in programming. One would be to remove each element from the original array as it is selected. The other would be to track the new elements, and see if one already exists before allowing the new selection. The former is faster in general, though the latter may be necessary if you have repeat elements in the input array, and don't want repeats in your output array.
If you don't have any repeats in the original array, you can do this quite easily in MATLAB. You use randperm to randomize the original array, then just pick the first n entries:
str = 'abcd';
newStr = str(randperm(numel(str)));
outStr = newStr(1:nletters);
Put that all inside the first loop, and you're good to go.
-Cam
3 件のコメント
Cam Salzberger
2017 年 10 月 12 日
Good point. I'll upvote your answer then, since it's more efficient, but leave mine here to help future searchers.
Thanks!
Maria K
2017 年 10 月 12 日
編集済み: Maria K
2017 年 10 月 12 日
2 件のコメント
Image Analyst
2017 年 10 月 12 日
Looks like a Latin Square. Are you wanting code to generate a Latin Square (used in statistical design of experiments)? https://en.wikipedia.org/wiki/Latin_square
James Tursa
2017 年 10 月 12 日
編集済み: James Tursa
2017 年 10 月 12 日
"... I just want one letter and it must be never the same ..."
But in your example above you do repeat the same letter. So what is it you really want? To cycle through all of the letters in a random fashion, and then start over? In that case, use the randperm( ) method that has already been suggested. Call randperm( ) once, use the letters from the result one-by-one until they are exhausted, then call it again etc.
Maria K
2017 年 10 月 12 日
編集済み: Maria K
2017 年 10 月 12 日
6 件のコメント
James Tursa
2017 年 10 月 12 日
編集済み: James Tursa
2017 年 10 月 12 日
My last response was intentionally posted as a comment since it was just tweaking your latest code. This site will not allow you to accept comments as answers. But my comment simply built upon the suggestions that were already made by others, who correctly had already pointed out that using randperm( ) was probably what you needed to use for your problem. If you look closely at the code I posted, it uses essentially the same basic logic as Cam's and Jan's posts. Hence my suggestion that you accept their answers.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!