Calculation of moving average in MATLAB

0

How to calculate the average of 12 months of a time series in MATLAB, so that, by setting the year of January to ten the year you want the average, the month of jan in year x is the average of Jan to ten in year x -1, the month of fev of the year x is the average of fev of the year x-1 to jan of the year x and the month sea of the year x will be the average of sea of the year x-1 to fev of the year x?

    
asked by anonymous 25.09.2014 / 20:48

1 answer

1

Unfortunately, we do not know how your data is structured, so to help you, let's assume your time series is something like:

Ano = 2014:
Janeiro = [1 2 7 8 9];
Fevereiro = [3 4 5];
Março = [5 2 5 0]
.
.
.
etc

The same principle would be for the year 2013, etc., etc.!

The first recommendation is to put your values in Structs , this will facilitate the manipulation of the data, we are talking about putting your data in this way:

A2014 = struct('1', [1 2 7 8 9], '2',[3 4 5],  '3',[5 2 5 0], '4',[9 2 0 0], '5',[33 1 5 90 200], '6',[82 4], '7',[5 2 5 0], '8',[31 6], '9',[9 0 3 4 5], '10',[2 0], '11',[7], '12',[2 9 0 3]);

A2013 = struct('1', [4 7 5], '2',[2 3 6 6],  '3',[22 3 4 5], '4',[1 3 3 5], '5',[9 6 5 5 20], '6',[1 3 5 4], '7',[4 0], '8',[1 5 6 6], '9',[3 5 5], '10',[1 4 4], '11',[2 7 5], '12',[5 5 5 1]);

I generated random and different size values, but that does not matter, it's just to show you the concept, notice that I assign each month with its corresponding number, each year receives a struct with the time series of each month.

Logic can be done in different ways, imagine the following entry:

anodesejado = XXXX
mesdesejado = X

The equation for walking between Months / Year would be (12 - (13-mesdesejado)) , if that calculation is equal to zero(0) walk from mesdesejado to month 12 of anodesejado-1 , if calculation is different from zero(0) walk from mesdesejado to month 12 of anodesejado-1 and 1 to (12 - (13-mesdesejado)) of anodesejado .

Of course, do not forget to add your values within each loop and in the end divide by 12 , so you have your average!

The logic described above in Matlab code follows:

%Entre com os valores desejados
anodesejado = 2014
mesdesejado = 1


A2014 = struct('1', [1 2 7 8 9], '2',[3 4 5],  '3',[5 2 5 0], '4',[9 2 0 0], '5',[33 1 5 90 200], '6',[82 4], '7',[5 2 5 0], '8',[31 6], '9',[9 0 3 4 5], '10',[2 0], '11',[7], '12',[2 9 0 3]);
A2013 = struct('1', [4 7 5], '2',[2 3 6 6],  '3',[22 3 4 5], '4',[1 3 3 5], '5',[9 6 5 5 20], '6',[1 3 5 4], '7',[4 0], '8',[1 5 6 6], '9',[3 5 5], '10',[1 4 4], '11',[2 7 5], '12',[5 5 5 1]);


soma=0;

if (12 - (13-mesdesejado)) == 0

  for M=mesdesejado:12

    soma = soma + sum(eval(["A" num2str(anodesejado-1)]).(num2str(M)));
  end
else
    for M=mesdesejado:12

    soma = soma + sum(eval(["A" num2str(anodesejado-1)]).(num2str(M)));
    end 
    for M=1:(12 - (13-mesdesejado))

    soma = soma + sum(eval(["A" num2str(anodesejado)]).(num2str(M)));
    end 



end

media = soma/12
    
27.09.2014 / 01:47