Is there a way to calculate a moving mean in a way that the values at the beginning and at the end of the array are averaged with the ones at the opposite end?
For example, instead of this result:
A=[2 1 2 4 6 1 1];
movmean(A,2)
ans = 2.0 1.5 1.5 3.0 5 3.5 1.0
I want to obtain the vector [1.5 1.5 1.5 3 5 3.5 1.0]
, as the initial array element 2 would be averaged with the ending element 1.
Generalizing to an arbitrary window size N
, this is how you can add circular behavior to movmean
in the way you want:
movmean(A([(end-floor(N./2)+1):end 1:end 1:(ceil(N./2)-1)]), N, 'Endpoints', 'discard')
For the given A
and N = 2
, you get:
ans =
1.5000 1.5000 1.5000 3.0000 5.0000 3.5000 1.0000
For an arbitrary window size n
, you can use circular convolution with an averaging mask defined as [1/n ... 1/n]
(with n
entries; in your example n = 2
):
result = cconv(A, repmat(1/n, 1, n), numel(A));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With