Saving loop outputs in a vector

3 visualizaciones (últimos 30 días)
Jarek
Jarek el 13 de Mayo de 2012
Respondida: ag el 4 de Dic. de 2024
Hi all,
I have the following question:
I have created a loop in matlab that basically does the following:
It runs a linear regression 200 times, starting with initial sample of 50 observations and increasing the sample by 50 observation for each of the next 200 repetitions.
I would like to do the following: Saving the outputs in a vector each time the regression runs and stacking them.
For example – one vector for R squared from each of the 200 regressions, one vector for F-statistic etc. Does anyone have a suggestion?
Thank you all for your time!

Respuestas (1)

ag
ag el 4 de Dic. de 2024
Hi Jarek,
To store the calculated states like "R-squared" or "F-Statistic" for each iteration, you can preallocate vectors.
The below code demonstrates how to do the same:
% Preallocate vectors to store regression outputs
r_squared_values = zeros(num_iterations, 1);
f_statistic_values = zeros(num_iterations, 1);
for i = 1:num_iterations
% logic for linear regression and calculate the required states needed
% to be saved
r_squared_values(i) = calculated_r_squared;
f_statistic_values(i) = calculated_f_statistic;
end
Hope this helps!

Categorías

Más información sobre Creating and Concatenating 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!

Translated by