Generating sequence of random numbers with a number of exceptions
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Karim
el 13 de En. de 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
Respuestas (1)
Luca Ferro
el 13 de En. de 2023
Editada: Luca Ferro
el 13 de En. de 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 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!