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!