Saving the outputs of multiple runs of a script

2 ビュー (過去 30 日間)
Rohan Soni
Rohan Soni 2021 年 5 月 17 日
コメント済み: Rik 2021 年 5 月 17 日
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!

採用された回答

VBBV
VBBV 2021 年 5 月 17 日
%if
Count = 0.1:0.1:0.9;
for correlation=1:length(Count)
Change this line in for loop and run it.
  2 件のコメント
Rohan Soni
Rohan Soni 2021 年 5 月 17 日
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 2021 年 5 月 17 日
@VBBV You should avoid using length. It is never the best choice. Either use numel or size with a dimension.

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2021 年 5 月 17 日
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;

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by