How to subtract mean daily values from hourly data?

2 views (last 30 days)
So I have flow data in an hourly time period, I was able to calculate the daily mean flow of these hourly values. Now I want to subtract the hourly flow values from the daily flow values to see how much change there is from the mean on a hourly basis.
Here is what I have so far:
Dates=datevec(DateandTime);
% Group by day
[unDates, ~, subs] = unique(Dates(:,1:3),'rows');
% Accumulate by day
AVG=[unDates accumarray(subs, Flow, [], @mean)];
f=AVG(:,4);
t=datenum(AVG(:,1:3));
figure
plot (x,y,t,f,'r')
legend('Hourly Flow','Daily Flow','location','best');
datetick ('x', 'mm-dd-yy', 'keepticks')
xlabel('Time');
ylabel('Flow [cfs]');
title('Daily Flow');
% axis tight
%%Difference from average flow
figure
n=datenum(AVG(:,1:3));
if n~=x
DeltaFlow=Flow-AVG(:,4);
end
I keep getting an error matrix do not agree.
  9 Comments
ccs
ccs on 18 Feb 2015
Edited: ccs on 18 Feb 2015
Ok. Geoff Hayes, thanks, that was too messy. I think this one below is more clear.
I want to generate a power consumption graph. I have measured data (power, energy) for every two seconds, for say three year.
Challenge: to write a time defined program that plots every measurement (scatter) for any given day or days (between 00:00 & 24:00).
I hope this is clear....requests for more clarity are welcome.
Thank you all
ccs
ccs on 18 Feb 2015
Sorry Geoff Hayes, I get what you meant now.
I created a new question, thanks for your guide.
Appreciated. :)

Sign in to comment.

Accepted Answer

Joseph Cheng
Joseph Cheng on 4 Sep 2014
Edited: Joseph Cheng on 4 Sep 2014
So, based on what you've done so far here is a spiffied up version:
%%generate sample data.
dateN = datenum('03-Sep-2014 15:26:10');
total15minsamps = 4*24*4; %4 days * 24 hours* 4 (15 min sec).
DATA = zeros(total15minsamps,7);
for ind = 1:total15minsamps %sample every 15 min.
data= 10+5*sin(2*pi/12*ind);
dateN = addtodate(dateN,15,'minute');
Dvect = datevec(dateN);
DATA(ind,:) = [Dvect data];
end
%%parse data
Dates = DATA(:,1:6);
Flow = DATA(:,end);
QuarterH_Time = datenum(DATA(:,1:end-1));
%%get hour's average flow
[unDates, HourSep, subs] = unique(Dates(:,1:4),'rows');
% Accumulate by hour
Hour_Data=[unDates accumarray(subs, Flow, [], @mean)];
Hour_AVG_flow=Hour_Data(:,5);
Hour_Time=datenum([Hour_Data(:,1:4) zeros(length(Hour_Data),2)]);
%%get day's average flow
[unDates, DaySep, subs] = unique(Dates(:,1:3),'rows');
% Accumulate by day
Day_Data=[unDates accumarray(subs, Flow, [], @mean)];
Day_AVG_flow=Day_Data(:,4);
Day_Time=datenum(Day_Data(:,1:3));
Day_Time = Day_Time + QuarterH_Time(1)-Day_Time(1); %offset so we don't start at the begining of the day
%%build subtraction matrix
DaySep = [DaySep;length(Flow)+1];
DeltaFlow = zeros(size(Flow));
for ind =1:length(DaySep)-1
POI = DaySep(ind):DaySep(ind+1)-1;
DeltaFlow(POI)=Flow(POI)-Day_AVG_flow(ind);
end
%%lets plot stuff
figure,plot(Hour_Time,Hour_AVG_flow,Day_Time,Day_AVG_flow,QuarterH_Time,DeltaFlow);
legend('Hourly Flow','Daily Flow','15 min data - day average','location','best');
datetick ('x', 'mm-dd-yy', 'keepticks')
xlabel('Time');
ylabel('Flow [cfs]');
title('Daily Flow');
Basically most of the stuff you can ignore because i needed to do some extra stuff to generate dummy data. But keep close attention to what i was doing with the DaySep array. Since we have two different length arrays that are based on the data i needed to use this value that is given by the unique() function to determine which indexes were deleted. With that i now know which 15 min data points correspond to which day.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!