random choice of elements for n times, without choosing the same element more than once
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Più risposte (3)
Cam Salzberger
il 12 Ott 2017
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 Commenti
Cam Salzberger
il 12 Ott 2017
Good point. I'll upvote your answer then, since it's more efficient, but leave mine here to help future searchers.
Thanks!
Maria K
il 12 Ott 2017
Modificato: Maria K
il 12 Ott 2017
2 Commenti
Image Analyst
il 12 Ott 2017
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
il 12 Ott 2017
Modificato: James Tursa
il 12 Ott 2017
"... 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
il 12 Ott 2017
Modificato: Maria K
il 12 Ott 2017
6 Commenti
James Tursa
il 12 Ott 2017
Modificato: James Tursa
il 12 Ott 2017
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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!