Saving loop outputs in a vector

3 Ansichten (letzte 30 Tage)
Jarek
Jarek am 13 Mai 2012
Beantwortet: ag am 4 Dez. 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!

Antworten (1)

ag
ag am 4 Dez. 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!

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by