Saving loop outputs in a vector

3 views (last 30 days)
Jarek
Jarek on 13 May 2012
Answered: ag on 4 Dec 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!

Answers (1)

ag
ag on 4 Dec 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!

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!