Correct way to calculate Moving Mean for Ozone hourly values
5 views (last 30 days)
Show older comments
Hello guys.
I have trouble understanding various "versions" of movmean. What I want to do is calculate moving mean with 8-hour interval from hourly ozone observations (see sample attached file).
Can anyone please explain what is the difference between
1) O3_movAv = movmean(data.O3,[0,8],'omitnan');
2) O3_movAv = movmean(data.O3, 8, 2, 'omitnan');
3) O3_movAv = movmean(data.O3,[8,0],'omitnan');
The output is different among these 3? Which is the correct one for my dataset?
Thank you in advance!
0 Comments
Accepted Answer
dpb
on 27 Jul 2021
1, 3. "movmean(A,[kb kf]) computes the mean with a window of length kb+kf+1 that includes the element in the current position, kb elements backward, and kf elements forward."
So 1) is "leading" average of 0 points back and 8 points forward, 3) is "trailing" average of 8 points back and 0 points forward
2. "movmean(A,k) returns an array of local k-point mean values, where each mean is calculated over a sliding window of length k across neighboring elements of A. When k is odd, the window is centered about the element in the current position. When k is even, the window is centered about the current and previous elements. ... movmean(___,dim) returns the array of moving averages along dimension dim for any of the previous syntaxes. For example, if A is a matrix, then movmean(A,k,2) operates along the columns of A, computing the k-element sliding mean for each row."
So, 2 is moving average of 8 points over the 8 points about the given position as described along the 2nd dimension of the array -- which would have to be a row vector in your case, making it a superfluous input.
4) They're all "correct"; just averaging over different sets of elements. Which is appropriate for your use (if any) is totally dependent upon what the intended use of the result is for. Only you can decide that...
3 Comments
Steven Lord
on 27 Jul 2021
It's not necessarily useful for this scenario since all your data is nicely sampled on the hour, but the meaning of k, kb, and kf shifts slightly if you have SamplePoints. In that case movmean(A, [kb, kf], 'SamplePoints', t) will not treat kb and kf as a number of elements but a distance in the SamplePoints vector.
t = [1 3 4 7 8]
x = 1:5
y = movmean(x, [1.5 0], 'SamplePoints', t)
In this case, y(2) only takes the mean of x(2) despite x(1) being only one element prior to x(2). This is because t(1) is less than t(2) minus 1.5.
This could be useful later on when you're working with messier data that may not be uniformly spaced. The red line segment with square ends in the picture below represents that second window in this case. The black segment with star ends represents the third window, which as you can see from the display of y above takes the average of the second and third x values.
plot(t, x, 'o')
hold on
plot(t(2) - [1.5, 0], [1.5 1.5], 'rs-')
plot(t(3) - [1.5, 0], [2.5 2.5], 'k-*')
More Answers (0)
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!