Clear Filters
Clear Filters

Generating sequence of random numbers with a number of exceptions

1 view (last 30 days)
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 Comment
Karim
Karim on 13 Jan 2023
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

Sign in to comment.

Answers (1)

Luca Ferro
Luca Ferro on 13 Jan 2023
Edited: Luca Ferro on 13 Jan 2023
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

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!