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

3 views (last 30 days)
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?

Accepted Answer

Torsten
Torsten on 6 Jan 2016
Maybe
weekdays = ts.data(daynumber>1 & daynumber<7);
weekenddays = ts.data(daynumber=1 | daynumber=7);
?
Best wishes
Torsten.
  1 Comment
jakob ekwall
jakob ekwall on 6 Jan 2016
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);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!