Home > Enterprise >  How do i convert monthly data into quarterly data in matlab as part of a table
How do i convert monthly data into quarterly data in matlab as part of a table

Time:09-24

How do I convert monthly data into quarterly data?

For example:

my 1 table looks like this:

TB3m
0.08
0.07
0.06
0.12
0.13
0.14

my second table is table of dates:

dates
1975/01/31
1975/02/28
1975/03/31
1975/04/30
1975/05/31
1975/06/30

I want to convert table 1 such that it takes the average of 3 months to give me quarterly data.

It should ideally look like:

TB3M_quarterly
0.07
0.13

so that it can match my other quarterly dates table which looks like:

dates_quarterly
1975/03/31
1975/06/30

Over all my data is from 1975 January to 2021 june which would give me around 186 quarterly data. Please suggest what I can use. It is thefirst time I am using matlab

CodePudding user response:

In case you can't understand a command, search for it in MATLAB documentation. One quick way to do so in Windows is to click on the function you don't understand and then press F1. For example, if you can't understand what calmonths is doing, then click on calminths and press F1.

TBm = [0.08; 0.07; 0.06; 0.12; 0.13; 0.14]; %values

% months
t1 = datetime(1975, 01, 31); %1st month
% datetime is a datatype to represent time
t2 = datetime(1975, 06, 30); %change this as your last month
dates = t1:calmonths(1):t2;
dates = dates'; %row vector to column vector

TBm_reshaped = reshape(TBm, 3, []);
TB3m_quarterly = mean(TBm_reshaped);
dates_quarterly = dates(3:3:end);
TB3m_quarterly = TB3m_quarterly';

T = table(dates_quarterly, TB3m_quarterly)

output

CodePudding user response:

I suggest you organize your data in a timetable, and then use the function retime()

% Replicating your data
dates = datetime(1975,1,31) : calmonths(1) : datetime(1975,6,30);
T = timetable([8 7 6 12 13 14]'./100, 'RowTimes', dates);

% Using retime to get quarterly values:
T_quarterly = retime(T, 'quarterly', 'mean')

Here you aggregate by taking the mean of the monthly data. For other aggregation methods, look at the documentation for retime()

  • Related