필터 지우기
필터 지우기

Sorting values from a time series into two new vectors depending on the day

조회 수: 2 (최근 30일)
First off, I'm not very skilled at Matlab but I have som experience.
So I basically want to sort hourly water consumption data values from a ts object into two new vectors, one with values from weekdays (mon-fri) and one with values from the weekend (sat&sun). The following code works but I've a hard time getting the results on the right format.
[daynumber]=weekday(datestr(ts.Time)) %weekdays have numbers 2,3,4,5,6 sat =7 & sun=1
weekdays=zeros(6264,1); %preallocation 6246 weekday and 2496 weekend hours in a year
weekenddays=zeros(2496,1);
for i=1:8760 %8760 hours in a year
if daynumber(i)>1 && daynumber(i)<7
weekdays(i,1)=ts.data(i,1);
else
weekenddays(i,1)=ts.data(i,1);
end
end
This gives me two vectors
  • Weekdays 8760x1 double
  • Weekenddays 8688x1 double
What I want is two vectors with continuous values (no zeros except when the consumption is zero). With the code above i get a bunch of zeroes in between the weekend values (zeroes, 48 weekend values in a row then 120 zeros then 48 weekend values and so on). Same goes for the weekday values but the other way around.
At first I thought I'd just remove the zeros in between but then I realised that in some cases the value from ts.data is zero.
Any ideas on how I can work my way around this problem?

채택된 답변

Torsten
Torsten 2016년 1월 6일
Maybe
weekdays = ts.data(daynumber>1 & daynumber<7);
weekenddays = ts.data(daynumber=1 | daynumber=7);
?
Best wishes
Torsten.
  댓글 수: 1
jakob ekwall
jakob ekwall 2016년 1월 6일
Thanks Torsten! This was a much better and easier way to do it.
For some reason
weekenddays = ts.data(daynumber=1 | daynumber=7);
Did not work When I changed it to the following it worked fine.
weekenddays = ts.data(daynumber==1 | daynumber==7);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by