functions to exclude repetitions

hello everyone, I want to know if there is a function that allows me, through a random variable, to run my code with all the elements of a matrix 1xn without repetition. here's an example of what i want to do:
Caos = [1:10]; %is my matrix
a = min(Caos); %minimum value of "Caos"
b = max(Caos); %maximum value of "Caos"
c = length(Caos);
d = 0; %counter
R = randi([a b],1); %random variable that's I don't want to repeat
while d<c
%execute my code
d=d+1
end

 채택된 답변

per isakson
per isakson 2013년 4월 4일

0 개 추천

Guess:
R2 = unique( R, 'stable' );
'stable' to avoid to sort R2

댓글 수: 3

Nicolò
Nicolò 2013년 4월 4일
ehm no, I want to keep the elements from array without the result's repetition. Like the balls with the numbers in lottery : you must take each time once until they finish.
It sounds like randperm() is what you want, then. This will randomly sort all of the integers between 1 and n, where n=10 in your example. You can then iterate through the elements of this vector, which by definition will not contain repeats (simulating your lottery-ball example).
doc randperm
Nicolò
Nicolò 2013년 4월 5일
randperm seems to be a good solution, i'll give it a shot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by