필터 지우기
필터 지우기

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