Generating sequence of random numbers with a number of exceptions
1 次查看(过去 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 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!