How can I compute the mean value over a time interval?

136 ビュー (過去 30 日間)
anna bicchi
anna bicchi 2021 年 4 月 23 日
コメント済み: Eric Sofen 2022 年 11 月 29 日
Hi veryone!
I have a matrix of 2 colums (one vector time, one vector values) and i'd like to compute the mean value over a time interval.
How can i do that?
thank you very much in advance

回答 (2 件)

Chad Greene
Chad Greene 2021 年 4 月 23 日
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:
plot(M(:,1),M(:,2))
xlabel time
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:
mean(M(ind,2))
ans = 3.5481e+03
  1 件のコメント
anna bicchi
anna bicchi 2021 年 4 月 26 日
Thank you very very much! :)

サインインしてコメントする。


Eric Sofen
Eric Sofen 2021 年 5 月 7 日
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
  2 件のコメント
Coral Stancliffe-Caldicott
Coral Stancliffe-Caldicott 2022 年 11 月 29 日
Hi,
If I were to have data once every 30 minutes for a year and wanted to calculate mean monthly values how would I adapt the above code?
Thanks
Eric Sofen
Eric Sofen 2022 年 11 月 29 日
retime(t,"monthly","mean")
The NewTimeStep positional argument lists the possible automatic aggregations around calendar unit. If you wanted to do something custom, you can also specify a new time vector. For example, if you wanted to average into 10-day bins, you might construct a time vector something like:
tvNew = t.Time(1):days(10):t.Time(end)
retime(t,tvNew,"mean")

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by