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

35 vues (au cours des 30 derniers jours)
Paige
Paige le 7 Nov 2023
Commenté : Mathieu NOE le 16 Nov 2023
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 commentaire
Dyuman Joshi
Dyuman Joshi le 7 Nov 2023
Use movmean with the appropriate method to treat the windows near endpoints.

Connectez-vous pour commenter.

Réponses (1)

Mathieu NOE
Mathieu NOE le 7 Nov 2023
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);

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by