Saving loop outputs in a vector

3 visualizzazioni (ultimi 30 giorni)
Jarek
Jarek il 13 Mag 2012
Risposto: ag il 4 Dic 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!

Risposte (1)

ag
ag il 4 Dic 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!

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by