How can I compute the mean value over a time interval?
131 views (last 30 days)
Chad Greene on 23 Apr 2021
Hi Anna, welcome to the forum.
With these types of questions it always helps if you can provide a minimal working example. But you explained the problem well enough that I think I can come up with one.
So you have a matrix M whose columns correspond to time and some dependent variable. Let's say M looks like this:
M = [(1:100)' (1:100)'.^2+randn(100,1)];
Plot the first and second colums to see what kind of data we're looking at:
To get the average over some interval, say between t = 55 and t=65, get the indices of that range:
ind = M(:,1)>=55 & M(:,1)<65;
Then calculate the mean of column 2 over the interval:
Eric Sofen on 7 May 2021
If you have, for example, data once per second for an hour and you want to calculate mean values for each minute, I'd recommend using a timetable to store the data and using the retime function. Something like this
% Synthetic data
t = timetable(seconds(0:3600)',rand(3601,1));
tAvg = retime(t,"minutely","mean");
tAvg.Time.Format = 'm' % Display times in minutes