Fred S
Fred S

Reputation: 1511

MATLAB: Averaging time-series data without loops?

I have measured a handful of variables in 30 minute intervals. Time stamps are available in datevec or datenum format. I want to calculate ...

a) ... daily averages and
b) ... average values at time x, e.g. temperature at 11:30, temperature at 12:00, etc. averaged over my whole dataset.

While this is, more or less, easily done with loops, I wonder if there is an easier / more convenient way to work with time-series, since this is a quite basic task after all?

/edit 1: As per request: click me for sample data

Upvotes: 3

Views: 2747

Answers (1)

Oleg
Oleg

Reputation: 10676

Considering that datevec() output is stored in tvec and data in x, group with unique(...,'rows') and accumulate with accumarray():

% Group by day
[unDates, ~, subs] = unique(tvec(:,1:3),'rows');

% Accumulate by day
[unDates accumarray(subs, x, [], @mean)]

% Similarly by hour
[unHours, ~, subs] = unique(tvec(:,4:5),'rows');
[unHours accumarray(subs, x, [], @mean)]

Upvotes: 6

Related Questions