How to create a list/array of output from Simulation
Show older comments
I've got a simulation, and I want to somehow count the number of times I get the value V = 100 through all my simulations.
At the moment I'm only doing 5 simulations because I just want proof of concept really.
So I have:
for sim=1:nTrials
SCS = [];
C = 1:100;
msize = numel(C);
Q = C(randperm(msize,100));
S = Q(1:10);
M = max(S);
R = Q(11:100);
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
end
where I want SCS to be the list.
I've been trying to either
a) make a list of every value of V I get, then count it (the counting is doable)
or
b) make a list of times I've gotten the value 100, almost like a tally.
I think a) is probably easier, but I can never get it to work for every iteration. I've tried this a few ways and I'm not quite sure what I'm doing. I've tried adding:
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
if V==100
SCS(nTrials) = V
end
end
which outputs
V = 93
V = 100
SCS = 0 0 0 0 100
V = 94
V = 84
V = 98
SCS = []
>>
Or
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
if V ==100
SCS = [SCS;A];
end
which outputs
V = 93
V = 100
V = 94
V = 84
V = 98
SCS = []
Answers (1)
"I want to somehow count the number of times I get the value V = 100"
OK:
count = 0;
nTrials = 5;
rng(11); % *
for sim=1:nTrials
C = 1:100;
msize = numel(C);
Q = C(randperm(msize,100));
S = Q(1:10);
M = max(S);
R = Q(11:100);
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
if V == 100
count = count+1;
end
end
disp(count);
If instead you want to keep track of which simulations generated V = 100 (not just how many):
SCS = [];
nTrials = 5;
rng(11); % *
for sim=1:nTrials
C = 1:100;
msize = numel(C);
Q = C(randperm(msize,100));
S = Q(1:10);
M = max(S);
R = Q(11:100);
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
if V == 100
SCS = [SCS; sim];
end
end
disp(SCS);
Or if you want to keep track of which simulation generated V = 100 and where it was in R:
SCS = [];
nTrials = 5;
rng(11); % *
for sim=1:nTrials
C = 1:100;
msize = numel(C);
Q = C(randperm(msize,100));
S = Q(1:10);
M = max(S);
R = Q(11:100);
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
if V == 100
SCS = [SCS; sim A];
end
end
disp(SCS);
(* The rng(11) is there so that the three different methods all operate on the same sequence of random numbers. You can leave it there for testing/debugging your code while you decide what it should do, but take it out when it's time to run real random simulations.)
2 Comments
nTrials=5;
RV = [10,20,30,40,50,60,70,80,90];
K = 100-RV;
NQ = RV+1;
% make a vector of counts, one count for each element of RV:
count = zeros(1,numel(RV));
% you can move these out of the loop(s), since they don't depend on RV and
% are the same for each simulation run:
C = 1:100;
msize = numel(C);
% loop over indexes, since you'll need to index NQ as well as RV:
for ii = 1:numel(RV)
disp([' RV = ' num2str(RV(ii))]);
for sim=1:nTrials
Q = C(randperm(msize,100));
S = Q(1:RV(ii));
M = max(S);
R = Q(NQ(ii):100);
if M==100
% V = R(1,90)
% You can't necessarily take the 90th element of R anymore,
% e.g., if RV(ii) is 50 then R has 50 elements. I changed
% it to take the last element of R. I don't know if this is
% what you want to do.
V = R(end)
else
A = find(R>M,1);
V = R(1,A)
end
if V == 100
count(ii) = count(ii)+1;
end
end
end
disp(count);
Categories
Find more on Matrices and Arrays 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!