How to calculate the Rolling Average or "moving mean" of a matrix.

34 ビュー (過去 30 日間)
Paige
Paige 2023 年 11 月 7 日
コメント済み: Mathieu NOE 2023 年 11 月 16 日
I have a 60x4 matrix and I am trying to find the "moving average" of 24 elements at a time. Attached is an example of what it would look like in Excel. In this example, smoke opacity values are recorded every 15 seconds for an 60 minute period (hence the 60x4 matrix). Then, a rolling 6 minute average is being taken for a full set of 24 values (6minutes X 4-15sec intervals). Once that operation is complete, I will use a 'max' function to find the highest rolling average. But I am having trouble doing the rolling average part!
Would a for - end loop be helpful in this situation? Should I preallocate the matrix (create a 60x4 matrix with 0s/1s and then import excel data)?
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 7 日
Use movmean with the appropriate method to treat the windows near endpoints.

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

回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 11 月 7 日
hello
see simple code below
I first created a dummy 2D array of 60 rows and 4 columns, but in fact it's just one vector of data so it's fairly easy to convert this 2D array to a 1D vector form , do the moving average stuff and then convert back the result from 1D to 2D array
%% create some dummy data
r = 60; % rows
c = 4; % columns
A = zeros(r*c,1); % vector
ind = randi([10,r*c-10],r*c/2,1);
A(ind) = 5;
AA = reshape(A,r,c); % 2D array
%% main code
% convert 2D array to vector
B = AA(:);
% apply movmean to vector
Y = movmean(B,24);
% convert back from vector to array
YY = reshape(Y,r,c);

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by