how to save the results obtained by a for loop repeated n times

4 views (last 30 days)
I have the function shown below that has some inputs: AC is a matrix 12x2 each row rapresents a month, in the first column are reported the monthly means of an events and in the second column there are their standard errors. As you can see my aim is to simulate from AC a trend for the numbers of years that i want but i have a problem. I need to save the results of the second loop, basically i want to save in simYR every value of simM that is reapeted for A times.
EG.
AC = [100 2; 110 4; 105 3; 130 7; 120 5; 115 5; 115 3; 140 7; 110 9; 105 4; 110 3; 120 5 ];
A = 3;
function [C] = Trend(AC,A)
simM = nan(1,12); % contains the values of a monthly simulation
simYR = nan(1,12*A); % contains the values of a monthly simulation also when the simulation is larger than one year
for year = 1:A
for month =1:12
simM(month) = randi([AC(month,1)-2*AC(month,2), AC(month,1)+2*AC(month,2)]);
end
end
end

Accepted Answer

David Fletcher
David Fletcher on 4 Apr 2021
Edited: David Fletcher on 4 Apr 2021
AC = [100 2; 110 4; 105 3; 130 7; 120 5; 115 5; 115 3; 140 7; 110 9; 105 4; 110 3; 120 5 ];
A = 3;
function [C] = Trend(AC,A)
simM = nan(1,12); % contains the values of a monthly simulation
simYR = nan(1,12,A); % Three dimensional array for storing each month as a 'page' in the array
for year = 1:A
for month =1:12
simM(month) = randi([AC(month,1)-2*AC(month,2), AC(month,1)+2*AC(month,2)]);
end
simYR(:,:,year)=simM %Each months figures are stored like a page in a book
end
end
  1 Comment
David Fletcher
David Fletcher on 4 Apr 2021
Edited: David Fletcher on 4 Apr 2021
You will of course have to return simYR from the function at the end, but the main idea is to firstly use a three dimensional array with the third dimension holding the figures for each month

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!