Clear Filters
Clear Filters

Time series - How to use a loop

3 views (last 30 days)
Stavroula Douva
Stavroula Douva on 20 Mar 2019
Answered: Jaynik on 17 Jul 2024
Hi, I'm working on a timeseries as you can see. I can canculate my results from 0-2 years and after that from 0-3 years until i reach the 20 years. Do you how to aytomatic this procces only by using a loop ?
The step to go from 0 year to 2 is by default 1 day.
Also i don't know how i can put my data in a loop so everytime i'm gonna use a loop fow an extra year there will be raised by 365.25 days

Answers (1)

Jaynik
Jaynik on 17 Jul 2024
Hi,
You can do the following to automate a for loop instead of writing manual code for each year:
a=1;
b=1;
sigma_white_noise= a;
sigma_flicker_noise= b;
T=1;
% Initialize an empty cell array to store your results
results = cell(1, 20);
% Loop over the years
for year = 2:20
t = 0:0.00273785: year;
% Initialize an empty cell array for the current year
A = cell(1, round(year * 365.25));
% Loop over the days in the current year
for i = 1:length(t)
A{i} = [ 1 t(i) (cos((2*pi()*t(i))/T)) (sin((2*pi()*t(i))/T))];
end
A = cell2mat(A');
N = length(t);
I = eye(N);
Cee = sigma_white_noise^2 * I;
Cx = inv(A' * inv(Cee) * A);
[sd, q] = cov2corr(Cx);
% Store the results for the current year
results{year} = struct('A', A, 'N', N, 'I', I, 'Cee', Cee, 'Cx', Cx, 'sd', sd, 'q', q);
end
This code will calculate the results for each year from 2 to 20 and store them in the results cell array. Each element of results is a structure containing the matrices A, N, I, Cee, Cx, and the vectors sd, q for the corresponding year. You can access the results for a specific year using results{year}. For example:
results{3}.A % gives matrix A for 3rd year
Please note that the round function is used to convert the number of days in a year to an integer, as the number of days in a year is not always a whole number due to leap years. This might cause slight discrepancies in the results, but they should be negligible for most purposes. For more precise results, you might need to take leap years into account in your calculations.
Hope this helps!

Categories

Find more on Loops and Conditional Statements 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!