average of the past 12 observations without a loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Danielle Leblance
am 5 Mär. 2017
Bearbeitet: Image Analyst
am 5 Mär. 2017
I have vector A. How can i create vector B with the same size of vector A where starting row 13 in B I compute the average of the previous 12 observations in A. In other words
B(13,1)=mean(A(1:12,1)
B(14,1)= mean(A(2:13,1)
etc...
I tried a loop and it is taking forever. I feel it could be done easily without a loop.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 5 Mär. 2017
Bearbeitet: Image Analyst
am 5 Mär. 2017
Try this:
m = randi(9, 1, 100);
windowWidth = 12;
kernel = [zeros(1, windowWidth), ones(1, windowWidth-1)]/windowWidth
slidingMean = conv(m, kernel, 'valid')
or else use movmean() if you have a recent version of MATLAB r2016a or later.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!