How to calculate average time over a given period?

22 views (last 30 days)
My time data (Time) is set up as a cell string with format dd/mm/yy HH:MM:SS. Currently I am filtering the data by a given step (Step) and have a script set up so that the time value at the end of that period will display. The script I am using for that is;
FilteredTime=Time(Step:Step:length(Time));
How do I need to change it so that the average time (or midpoint in time) over this defined step is output rather than the end time?

Answers (1)

Robert
Robert on 6 Jul 2016
You could
  1. convert your times to numeric values using datenum
  2. trim your times to an even multiple of step
  3. reshape the array of times to have step rows
  4. average across the rows
Time = cumsum(randi(25,101,1)); % dummy values for the example
step = 5;
% We will take the average of every set of 5 and will ignore the last point
% since it doesn't belong to any set of 5.
Time = Time(1:floor(end/step)*step);
% A compact way to take the average of every set
avg = mean(reshape(Time,step,[]));
% Let's take a look!
plot(1:length(Time),Time,step/2:step:length(Time),avg,'o-')

Products

Community Treasure Hunt

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

Start Hunting!