time series 30 year sliding window

I have a set of daily data that ranges from the years 1772-2009. The data is currently in an array that has one column as the date number and one column as my data. I want to set 30 year sliding windows for this data moving a year on every time. For example I want the data to be in an array where the first column has all the data from 1772-1801, the 2nd has all the data from 1773-1802 and so on.
Each value has a corresponding date to it (year, month and day). It would be nice to be able to program MATLAB to take all the values corresponding to the years in the ranges I need.
For example I want to say something along these lines, I just dont know the exact code or if what I want to do is even possible: I already have data = my daily data
for y=1772:1979
30yearperiods(:,1,2,3...207) = temp (dependent on year== n to n+30)
Does anyone know how I can do this?
I have had major problems trying to set for loops that encompass the 30 year periods including all the leap years etc and its frustrating having to do this when I have the data I need and I just can't extreact it

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 2일

1 개 추천

Since not all 30-year periods contain the same number of dates (due to leap year), you'll need to use cell array to have the 30-year moving window data. It's a matter of utilizing the datenum() function.
clear;
%create an example data
StartYear=1772;
EndYear=2009;
OrigData=datenum(StartYear,1,1):(datenum(EndYear,1,1)-1);
OrigData=[OrigData',(1:length(OrigData))'];
%constrct the desired data
Period=30;
Data=cell(EndYear-StartYear-Period+1,1);
for Year=StartYear:(EndYear-Period)
StartCount=datenum(Year,1,1)-datenum(StartYear,1,1)+1;
DataCount=datenum(Year+Period,1,1)-datenum(Year,1,1);
Data{Year-StartYear+1}=OrigData(StartCount:(StartCount+DataCount-1),2);
end
>> OrigData(end,:)
ans =
733773 86563
>> Data{end}(end)
ans =
86563

댓글 수: 3

James
James 2011년 10월 3일
Ok so I have used what you have said and have the data in a cell array, but I'm unfamiliar with the cell function. How do I use the data now its in this array? All I get when I select one of the periods is:
>> Data(1,1)
ans =
[10957x1 double]
And also why do you define 2 columns in OrigData?
Data{Year-StartYear+1}=OrigData(StartCount:(StartCount+DataCount-1),2);
James
James 2011년 10월 3일
Like I need a list of the values in the array so I can detrend, take the mean etc?
Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
To reference the 2nd data in your first 30-year period, use Data{1}(2),
100th data in the 3rd 30-year period, Data{3}(100).
Your original data has two columns, right? The first column is just date, the second column is your true data, temperature, for example. I just use a serial of numbers to mimic the second column data.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

bahman rostami tabar
bahman rostami tabar 2011년 12월 20일

0 개 추천

you can easily use this code:
% where data is your column data vector
% m is window length so for your case m=30;
OverlapAggregateVector=zeros(m, size(data,1)-m+1);
for k=1:m
OverlapAggregateVector(k,:)=data(k:end-m+k,1);
end

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2011년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by