Calculation of moving average in MATLAB

Asked

Viewed 1,851 times

0

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

1 answer

1

Unfortunately we don’t know how your data is structured, so to help you we’ll assume that 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 data manipulation, we are talking about putting your data 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 values and different sizes, but that doesn’t matter, 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 the months/year would be (12 - (13-mesdesejado)), if the calculation is equal to zero(0) walk from mesdesejado up to the month 12 of anodesejado-1, if the calculation is different from zero(0) walk from mesdesejado up to the month 12 of anodesejado-1 and of 1 until (12 - (13-mesdesejado)) of anodesejado.

Of course don’t forget to add your values inside each loop and at the end divide by 12, so you’ll get your average!

Follows the logic described above in Matlab code:

%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
  • Ederwander, thank you for your reply, I will try to implement it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.