# Calculate average over each specific interval

3 ビュー (過去 30 日間)
Saad Alqahtani 2021 年 7 月 7 日
コメント済み: Saad Alqahtani 2021 年 7 月 8 日
Hi,
I'm trying to calculate the mean every 5 seconds of data till the end.
so x= (1,63369) % my data in Newton Meter which is about 30.9419 seconds long
t = 5*2048 % 5 seconds interval.
I'd like to have your help in (1- how to make it calculate the mean in increaments from start till the end 2- how to deal with the last posrtion of the data that shorter than 5 seconds)
any help would be appreciate it. Thanks in advance.
##### 2 件のコメント表示非表示 1 件の古いコメント
Saad Alqahtani 2021 年 7 月 7 日

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

### 採用された回答

dpb 2021 年 7 月 7 日

2) first -- how to deal with the last posrtion of the data that shorter than 5 seconds"
How do you WANT to deal with it? You can either ignore it and have fix(numel/5) samples or average the values that do have in the last segment -- it's your choice.
1) The classic MATLAB way is to reshape() and use mean() --
a) Ignore last partial segment --
N=5; % number elements to average over
mnX=mean(reshape(x(1:N*fix(numel(x)/N)),N,[])); % average over N elements ignoring end values
b) Deal with odd end
x=[x nan(1,N*ceil(numel(x)/N)-numel(x))]; % augment vector to make even
mnX=mean(reshape(x,N,[]),1,'omitnan'); % average over N elements ignoring NaN elements
##### 3 件のコメント表示非表示 2 件の古いコメント
Saad Alqahtani 2021 年 7 月 8 日
(b) works now as well. It looks like I had some typo somewhere. Thank you so much. I really appreciate your help!

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

### Community Treasure Hunt

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

Start Hunting!