how to save the results obtained by a for loop repeated n times
1 回表示 (過去 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
0 件のコメント
採用された回答
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
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 Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!