how to save the results obtained by a for loop repeated n times
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Francesco Grechi
el 4 de Abr. de 2021
Editada: David Fletcher
el 4 de Abr. de 2021
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 comentarios
Respuesta aceptada
David Fletcher
el 4 de Abr. de 2021
Editada: David Fletcher
el 4 de Abr. de 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 comentario
David Fletcher
el 4 de Abr. de 2021
Editada: David Fletcher
el 4 de Abr. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!