This example might help you find a solution to your specific problem, i.e. "I don't know how to calculate an hourly average from my five minute samples"
Create an irregular time series with time steps typically
sdn = now + 6*sort( rand( 1000, 1 ) );
val = randn( size( sdn ) ) + sin( sdn );
Create points in time of an hourly time series
sdn1 = floor( sdn(1) );
sdn2 = ceil( sdn(end) );
sdn_hourly = bsxfun( @plus, (sdn1:sdn2), transpose((0:23)/24) );
sdn_hourly = sdn_hourly(:);
Reduce the irregular time series to a regular hourly time series
[ ~, ixs ] = histc( sdn, sdn_hourly );
valh = accumarray( ixs, val, size( sdn_hourly ), @mean, nan );
.
--- [EDIT, 2012-11-02]: in response to comment ---
Averaging of regular time series causes some problems because of
- floating point precision; a rounding error may cause "13:00:00" to be included in the previous OR the succeeding "hourly interval"
- definition of time intervals. We want an half-closed interval because one value, e.g. "13:00:00", must not be included in two "hourly intervals".
My solution to these two problems are
- use seconds and whole numbers to represent time; I use serial-second-number, ssn. The resolution of one second is good enough for me. Before "ssn" my code was littered with "abs(sdn-sdn1)<eps" and similar.
- use the half-closed interval, [t1,t2). t such that t1<=t<t2 is in the interval. The function, histc, supports the half-open interval [). I let t1 either be a point in time or the time interval, [t1,t2). (The colon operator and linspace returns closed intervals: both the endpoints are included.)
Now you ask for
- the half-closed interval (t1,t2] and
- that t2 is used as timestamp of the interval, (t1,t2]
I cannot see a good solution to that. Here is my experiment. The last three lines of code calculates hourly averages according to your requirements. (Replace @sum by @mean and do the testing that I haven't done.)
Create an regular time series with 5-minute time steps
vec = datevec( now );
sdn_5m = datenum(vec(1),vec(2),vec(3),0, transpose(0:5:N*24*60-0.01), 0 );
val_5m = ones( size( sdn_5m ) );
Create points in time of an hourly time series
sdn1 = floor( sdn_5m(1) );
sdn2 = ceil( sdn_5m(end) );
sdn_hourly = bsxfun( @plus, (sdn1:sdn2), transpose((0:23)/24));
sdn_hourly = sdn_hourly(:);
Reduce a regular 5-minute series to a regular hourly time series [t1,t2)
[ ~, ixs ] = histc( sdn_5m, sdn_hourly );
val_5m_h1 = accumarray( ixs, val_5m, size( sdn_hourly ), @sum, nan );
assert( all( val_5m_h1(not(isnan(val_5m_h1)) ) == 12 ), 'a:b:c' ...
, 'Not all hourly sum are equal to twelve' )
half_a_sec = 1/(24*60*60*2);
[ ~, ixs ] = histc( sdn_5m - half_a_sec, sdn_hourly );
val_5m_h2 = accumarray( ixs+1, val_5m, size( sdn_hourly ), @sum, nan );