End Points on movmean don't look right

7 次查看(过去 30 天)
If you run the simple code (see end of post) you are taking the sliding window filter using matlab "movmean". Per the description:
"The window size is automatically truncated at the endpoints when there are not enough elements to fill the window. When the window is truncated, the average is taken over only the elements that fill the window. M is the same size as A."
If I read this correctly-for element 1 of the dataset, only element 1 "fills the window". Thus the average of A(1) is just A(1)/1 =1. Looking at my plot I do not see this--it appears as thought the first element is wrong--instead of 1, movmean returns 1.5.
What did I miss here?
Thanks--Fritz
========================================
for i=1: 10
A(i)=i;
end
for i=11: 20
A(i)=20-i;
end
plot(A,'b','LineWidth',2);
M=movmean(A,3)
M = 1×20
1.5000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 9.3333 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000 0.5000
hold on;
plot(M,'k','LineWidth',2);
fprintf("A(1)=%f\n",A(1)) ;
A(1)=1.000000
fprintf("M(1)=%f\n",M(1)) ;
M(1)=1.500000
fclose all;

采纳的回答

Matt J
Matt J 2021-10-2
编辑:Matt J 2021-10-2
If I read this correctly-for element 1 of the dataset, only element 1 "fills the window".
No, the length 3 window is centered on element 1, so element 2 will also be in the window. The value of 1.5 is the average of [1,2]. If you don't want the current element to lie at the right hand edge fo the window, you can do
movmean(A,[2,0])

更多回答(2 个)

Image Analyst
Image Analyst 2021-10-2
When the center of your 3-element window is over element 1 of A, which is
A =
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0
the values 1 and 2 are inside the window.
The window does not travel so far that the center of the window is off the left side of A and only the tail overlaps the first element of A. If it did, then the output matrix would be larger than A.
So, the average of 1 and 2 is 1.5, like it is for M.

Fritz Sonnichsen
Fritz Sonnichsen 2021-10-2
OK--I was placing my window wrong--thanks for the replies
(and sorry for the bad English in my title! FInger error)
cheers
Fritz
  1 个评论
Matt J
Matt J 2021-10-2
Glad you worked it out, but please Accept-click one of the answers, so that Mathworks has a record that a resolution was reached.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by