Generating sequence of random numbers with a number of exceptions
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi, I am working with a data set which has a number of timepoints - these are stored in a variable. I wish to generate a sequence of (8) random timepoints but exclude values which are stored in the variable mentioned above. Any help to do this will be appreciated.
댓글 수: 1
Karim
2023년 1월 13일
you can use the function ismember to check if one of the random timepoints is stored in the variable, if it would be the case you can then replace that point with a new random point
답변 (1개)
Luca Ferro
2023년 1월 13일
편집: Luca Ferro
2023년 1월 13일
could you clarify what do you mean by timepoint?
once you do that i will tweak my suggestion accordingly. For now:
eightRandTimePoints=zeros(1,8); %preallocating for speed
ccount=0; %initialize counter
for jj=1:8
newTimePoint= ... %here a would generate a random time point if it was clearer what a timepoint is
if ~ismember(newTimePoint,avoidList) %if the newly generated timepoint is not part of the avoid list, store it
eightRandTimePoints(jj)=newTimePoint;
ccount=0; %reset security counter
else
jj=jj-1; %if it has to be avoided, we need to run the index back by one t gurantee that we get 8 and not less
if ccount=5 %this is just a counter to avoid infinite loops, if it fails to store 5 consecutives times it exits the cycle
return;
else
ccount=cccount+1;
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!