Saving loop outputs in a vector

3 vues (au cours des 30 derniers jours)
Jarek
Jarek le 13 Mai 2012
Réponse apportée : ag le 4 Déc 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!

Réponses (1)

ag
ag le 4 Déc 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!

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by