Saving the outputs of multiple runs of a script

2 vues (au cours des 30 derniers jours)
Rohan Soni
Rohan Soni le 17 Mai 2021
Commenté : Rik le 17 Mai 2021
I have a stochastic simulation which I would like to run multiple times while changing a variable called 'correlation'.
For each run I would like to save the outputs as individual matrices so I can plot them together and compare values.
How would I go about saving the outputs of each simulation for each corresponding value of correlation?
I tried incorporating this into a 'for' statement but I'm very new to MATLAB and get the error "Array indices must be positive integers or logical values."
My code so far is:
R01= zeros(length(t),maxit);
R02= zeros(length(t),maxit);
R03= zeros(length(t),maxit);
R04= zeros(length(t),maxit);
R05= zeros(length(t),maxit);
R06= zeros(length(t),maxit);
R07= zeros(length(t),maxit);
R08= zeros(length(t),maxit);
R09= zeros(length(t),maxit);
for correlation=(0.1:0.1:0.9)
run stochasticsimulation.m
ResultsR01(:,correlation)= R01;
ResultsR02(:,correlation)= R02;
ResultsR03(:,correlation)= R03;
ResultsR04(:,correlation)= R04;
ResultsR05(:,correlation)= R05;
ResultsR06(:,correlation)= R06;
ResultsR07(:,correlation)= R07;
ResultsR08(:,correlation)= R08;
ResultsR09(:,correlation)= R09;
end
Could you please advise on either where to look to learn this stuff or what improvements I can make to my code.
Thanks!

Réponse acceptée

VBBV
VBBV le 17 Mai 2021
%if
Count = 0.1:0.1:0.9;
for correlation=1:length(Count)
Change this line in for loop and run it.
  2 commentaires
Rohan Soni
Rohan Soni le 17 Mai 2021
Thank you for your submission! I tried doing this but then received an error saying that the indices of left side not compatible with size of right side?
Not sure why this is happening but I feel like it may be to do with the way the script I'm calling is outputting results.
Rik
Rik le 17 Mai 2021
@VBBV You should avoid using length. It is never the best choice. Either use numel or size with a dimension.

Connectez-vous pour commenter.

Plus de réponses (1)

Rik
Rik le 17 Mai 2021
You should not be using a script outside of debugging. If you're only interested in the value of a single variable, you should make your script a function with that variable as the output. Then you can run it in a loop.
You should also note that indices in an array must be positive integers:
my_array(0.1) % this is an invalid syntax
my_array(1) % this is a valid syntax
You should also avoid numbering your variables, use indexing instead. If you only store a single value:
%change this
R01=my_function;
R02=my_function;
R03=my_function;
%to this:
R(1)=my_function;
R(2)=my_function;
R(3)=my_function;

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by