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

3 次查看(过去 30 天)
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

采纳的回答

David Fletcher
David Fletcher 2021-4-4
编辑:David Fletcher 2021-4-4
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 个评论
David Fletcher
David Fletcher 2021-4-4
编辑:David Fletcher 2021-4-4
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by