Generating sequence of random numbers with a number of exceptions
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Karim
il 13 Gen 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
Risposte (1)
Luca Ferro
il 13 Gen 2023
Modificato: Luca Ferro
il 13 Gen 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
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!