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 CenterFile Exchange에서 Loops 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