How do I get the average of data that are divided into different time intervals?

1 view (last 30 days)
Hi, I´m working on a project where i have two sets of data: one has output data divided into time of output, and the other has input data divided into hourly figures. Example:
--------------------------------------------------------------------
% A=[year month day hour minute second data-output;...]
A = [ 2013 1 1 5 4 0 151; 2013 1 1 7 38 0 121; 2013 1 1 10 28 0 144; ...]
% B=[data-input year month day hour minute second;...]
B= [ 7.7 2013 1 1 0 0 0; 7.9 2013 1 1 1 0 0; 7.8 2013 1 1 2 0 0;...]
--------------------------------------------------------------------------
As you can see the time intervals don´t match and my goal is to get the average value of the "data-inputs" at every data-output. For example: I used (7.7+7.9+7.8...)/n to obtain 151 as an output. Does anyone know how? I'm pretty bad at explaining things so please ask if you didn't understand something.

Accepted Answer

Jon
Jon on 23 Aug 2013
Edited: Jon on 23 Aug 2013
To restate what you posted, you have a set of data consisting of (value, input_time) pairs. At some interval, you want to report the average of the values since the last report so you end up with an array consisting of (ave_value, output_time).
First, convert your time values to a number using datenum. Then use find to find the input times between the output times, extract the input values corresponding to those input times, and compute the average.
% A = [ 2013 1 1 5 4 0; 2013 1 1 7 38 0; 2013 1 1 10 28 0; ...] % the output times
% B = [7.7 2013 1 1 0 0 0; 7.9 2013 1 1 1 0 0; 7.8 2013 1 1 2 0 0;...] % the input values and times
AA=[0;datenum(A)]; % the output times as numbers
C=datenum(B(:,2:7)); % the input times as numbers
D=zeros(size(A,1)); % initialize the output vector
for k=1:length(AA)-1
% get the indices for the input times that fall between the current
% and next output times.
ind=find(C>AA(k) & C<=AA(k+1));
% compute the average of the values corresponding to these times
D(k)=mean(B(ind,1)); % the output values
end
D now contains the average values of B for the output times A. Note that the first element of AA needs to be less than the minimum input time. If you want the average since the start of the data instead of since the last report, leave out the first test in the find statement

More Answers (0)

Categories

Find more on Dates and Time 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!