How to create a list/array of output from Simulation

4 vues (au cours des 30 derniers jours)
Lou
Lou le 21 Avr 2022
Commenté : Voss le 23 Avr 2022
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 = []

Réponses (1)

Voss
Voss le 22 Avr 2022
"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
V = 87
V = 93
V = 100
V = 91
V = 100
disp(count);
2
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
V = 87
V = 93
V = 100
V = 91
V = 100
disp(SCS);
3 5
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
V = 87
V = 93
V = 100
V = 91
V = 100
disp(SCS);
3 18 5 11
(* 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 commentaires
Lou
Lou le 23 Avr 2022
Modifié(e) : Lou le 23 Avr 2022
Thank you!
If I want to run multiple values of RV and count V=100 for each value of RV how do I do this?
clear all
nTrials=5;
RV = [10,20,30,40,50,60,70,80,90]
K = 100-RV
NQ = RV+1
for RV = [10,20,30,40,50,60,70,80,90]
for sim=1:nTrials
C = 1:100;
msize = numel(C);
Q = C(randperm(msize,100));
S = Q(1:RV);
M = max(S);
R = Q(NQ:100);
if M==100
V = R(1,90)
else
A = find(R>M,1);
V = R(1,A)
end
end
end
I can keep a running count and that's great, but I would like to know how many times when RV = 10 I get V=100, and then RV=20, and then RV=30 and so on.
I've tried swapping the for RV and for sim round but it still doesn't work.
I also tried adding individual bits but this doesn't seem to work either.
if RV == 10
if V==100
count10 = count10+1;
end
end
if RV == 20
if V==100
count20 = count20+1;
end
end
if RV == 30
if V==100
count30 = count30+1;
end
if RV == 40
if V==100
count40 = count40+1;
end
end
Voss
Voss le 23 Avr 2022
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
RV = 10
V = 99
V = 96
V = 98
V = 98
V = 100
RV = 20
V = 56
V = 100
V = 93
V = 100
V = 100
RV = 30
V = 99
V = 100
V = 96
V = 99
V = 100
RV = 40
V = 100
V = 88
V = 100
V = 100
V = 99
RV = 50
V = 27
V = 86
V = 98
V = 1
V = 99
RV = 60
V = 5
V = 100
V = 10
V = 97
V = 69
RV = 70
V = 51
V = 42
V = 88
V = 99
V = 52
RV = 80
V = 47
V = 12
V = 42
V = 92
V = 18
RV = 90
V = 11
V = 100
V = 49
V = 12
V = 47
disp(count);
1 3 2 3 0 1 0 0 1

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by