필터 지우기
필터 지우기

How to repeat a random number generation and simulation for 1000 times

조회 수: 1 (최근 30일)
Hanson Zhou
Hanson Zhou 2015년 10월 14일
답변: Guillaume 2015년 10월 14일
Hi all, I have a code for random number generation and simulation for queuing experiment. The code is as below
code:
time=6;
lambda=5;
mean_arr=1/lambda;
num_arr=time*lambda*5;
interval=exprnd(mean_arr,1,num_arr);
intv(1,:)=interval;
arr_time(1,:)=cumsum(intv(1,:));
num_vaild=sum(arr_time(1,:)<=time);
data(1,:)=arr_time(1:num_vaild);
mean_treat=1/4;
std_treat=1/6;
data(2,:)=normrnd(mean_treat,std_treat,1,num_vaild);
for j=1:num_vaild
if data(2,j)<0
data(2,j)=0;
end
end
data(3,1)=0;
data(4,1)=data(1,1)+data(2,1);
patient=[1];
for i=2:num_vaild
queue=sum(data(4,patient)>data(1,i));
if queue==0;
data(3,i)=0;
data(4,i)=data(1,i)+data(2,i);
patient=[patient,i];
else num_patient=length(patient);
data(3,i)=data(4,num_patient)-data(1,i);
data(4,i)=data(4,num_patient)+data(2,i);
patient=[patient,i];
end
end
doctor_leavingtime = data(4,num_vaild);
I need to repeat this entire script for 1000 times and record the doctor's leaving time in data(4,end) every time, and create a vector for them.
any idea about how to repeat?

답변 (1개)

Guillaume
Guillaume 2015년 10월 14일
doctor_leavingtime = zeros(1, 1000);
for repeat = 1:1000
%your code to repeat
%...
doctor_leavingtime(repeat) = data(4, num_valid);
end
Unless I misunderstood your question that's all you have to do: wrap your original code in a loop.

카테고리

Help CenterFile Exchange에서 Optimization Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by