MATLAB Answers

How to convert a time serie with daily data to a time serie with weekly data by taking the average of every 7 values of the daily vector?

1 ビュー (過去 30 日間)
I have a vector with daily data. I need to average every 7 values of it to obtain a new vector with weekly data.

  2 件のコメント

Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 10 日
Sure!
%Time Vector
xtime1= [1:1:6000]'; %daily
xtime2= [1:1:42000]'; %weekly
%Time Series 1- daily entries
%comand randn generats 42000 random numbers that are normaly distributed.
rng('default')
time_series1=randn(42000,1);
%generating time series with 6000 entries log-normal distributed
%the code for this distribution is based on the code available at
%mathworks.com
rng('default'); % So that numbers can be repeated
time_series2 = lognrnd(0,0.25, [6000 1]); %generating time series with mu set to zero and sigma 0.25

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 11 日
As you mentioned in your comment, you have a vector of type double. In that case, you can average 7 elements as follow:
x = rand(1000,1); % random data
x = padarray(x, [ceil(numel(x)/7)*7 - numel(x) 0], 0, 'post'); % make sure that number of elements are multiple of 7
mean_x = mean(reshape(x, 7, []), 1)';

  3 件のコメント

Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
Is the second x a new vector? or am I re arranging the first x vector?
Is this code correct?
rng('default')
time_series1 = normrnd(0,1,42000,1); %daily time series multiple of 7, 42000
time_series3 = padarray (time_series1, [ceil (numel (time_series1) / 7) * 7 - numel (time_series1) 0], 0, 'post' ); %
mean_ts3 = mean (reshape (time_series3, 7, []), 1);
Ameer Hamza
Ameer Hamza 2020 年 3 月 12 日
Fernanda, the second x does not need to be the same, your code is correct. But you will get an error because you are giving extra spaces between function name and the input arguments. This is generally not a problem, but it will cause an error when done inside [ ] brackets. So change the third line
time_series3 = padarray (time_series1, [ceil(numel (time_series1) / 7) * 7 - numel(time_series1) 0], 0, 'post' );
Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
Thank you Ameer!
Now I have a vector of 1 row and 6,000 columns.

サインインしてコメントする。

その他の回答 (1 件)

Stephan
Stephan 2020 年 3 月 10 日
編集済み: Stephan 2020 年 3 月 10 日
The usual way to do this is by using the retime function.

Community Treasure Hunt

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

Start Hunting!

Translated by