Calculation of the moving mean for the first items

조회 수: 9 (최근 30일)
Angel Lozada
Angel Lozada 2024년 10월 5일
댓글: Angel Lozada 2024년 10월 7일
Hello.
I am trying to understand the calculations regarding to movmean command.
Let say I have the following array, and I want to calculate the movmean with a windowsize of 2
A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movmean(A,2)
M = 1×10
4.0000 6.0000 7.0000 2.5000 -1.5000 -2.5000 -2.0000 1.0000 3.5000 4.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Within the result, I do not understand how I got the first element 4.0000 (i.e., the first iteration)
Now, let say I use a windowsize of 3
A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movmean(A,3)
M = 1×10
6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000 4.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I do not understand how I got the first element 6.0000 (i.e., the first iteration)
Now, let say I use a windowsize of 4
A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movmean(A,4)
M = 1×10
6.0000 6.0000 4.2500 2.7500 0 -1.7500 -0.7500 0.7500 2.7500 4.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I do not understand how I got the first two elements 6.0000 and 6.0000 (i.e., the first two iteration)
So forth untill a windowsize of 10.
I appreciated you cooperation.

채택된 답변

Venkat Siddarth Reddy
Venkat Siddarth Reddy 2024년 10월 5일
편집: Venkat Siddarth Reddy 2024년 10월 5일
Hi Angel,
The "movmean" function calculates and returns an array of mean values, with each mean computed over a sliding window of specified length, "windowsize." The position of the sliding window is determined by the current element for which the mean is being calculated and the provided "windowsize."
In the first scenario, the "windowsize" provided is an even number. Therefore, the sliding window positions its center between the "current" element and the element before the "current" element.
To calculate the mean for first element in the array, the sliding window position would be as follows:
[4 8 6 -1 -2 -3 -1 3 4 5]; %Array
% | | --> Indicates the array values position in the sliding window
%[1 2]
As there is only one element in the sliding window the mean value would be
In the second sceanrio, the "windowsize" provided is odd. Therefore, the sliding window positions its center at the current value. The position of sliding window for the first element in the array would be as follows:
[4 8 6 -1 -2 -3 -1 3 4 5]; %Array
% | | | --> Indicates the array values position in the sliding window
%[1 2 3]
There are 2 elements in sliding window, the mean value would be
And the third scenario is similar to the first scenario. The sliding window with "windowsize" of value 4 would be positioned as follows:
For the first element
[4 8 6 -1 -2 -3 -1 3 4 5]; %Array
% | | | | --> Indicate the array values position in the sliding window
%[1 2 3 4]
The mean would be:
For the second element
[4 8 6 -1 -2 -3 -1 3 4 5]; %Array
% | | | | --> Indicate the array values position in the sliding window
%[1 2 3 4]
The mean would be:
To learn more about the "movmean" function, please refer to the following documentation:
I hope this clarifies how the "movmean" function performs the "moving mean".
  댓글 수: 1
Angel Lozada
Angel Lozada 2024년 10월 7일
Dear Venkat.
Thanks for your support and cooperation.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Torsten
Torsten 2024년 10월 5일
이동: Image Analyst 2024년 10월 6일
From the documentation:
Endpoints — Method to treat windows near endpoints
"shrink" (default) | "discard" | "fill" | numeric or logical scalar
Method to treat windows near endpoints, specified as one of these options:
ValueDescription
"shrink"Shrink the window size near the endpoints of the input to include only existing elements.
"discard"Do not output any average values when the window does not completely overlap with existing elements.
"fill"Replace nonexisting elements with NaN.numeric or logical scalarReplace nonexisting elements with the specified numeric or logical value.
  댓글 수: 1
Angel Lozada
Angel Lozada 2024년 10월 7일
Dear Tasten.
Thanks for your support and cooperation.

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2024년 10월 5일
편집: Bruno Luong 2024년 10월 6일
"M = 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. 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."
Explanation with code that emulates how movmean works, put a debugger break point at the line M(i) = sum(Aiwin) / ni; and check why it get its value.
A = [4 8 6 -1 -2 -3 -1 3 4 5]
A = 1×10
4 8 6 -1 -2 -3 -1 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 2:4
assert(mod(k,1) == 0 && k >= 1, 'k must be positive integer')
% Moving windows, "left" element to the left of center
% "right" element to the right, both are integers, and contains k numbers
hw = (k-1)/2;
if mod(hw,1) == 0.5 % or mod(k,2) == O or k is even
left = hw + 0.5; % round it up, or ceil(hw)
% left is right+1 in this case
else
left = hw; % left == right in this case of k is odd
end
right = k - left - 1; % so that left + right = k-1
n = length(A);
M = zeros(size(A)); % allocate the resulting array
for i = 1:n % Loop on elements of A and fill corresponnding M
% moving window left and right index bracket
li = max(i-left,1); % max to truncate left size if overflowed
ri = min(i+right,n); % min to truncate right size if overflowed
Aiwin = A(li:ri); % extract moving window data
ni = length(Aiwin); % == (ri-li+1); % effective number of sliding windows centered at i
M(i) = sum(Aiwin) / ni; % == mean(Auwin), compute the mean
end
% display the result for specific window length k
fprintf('k = %d, M = %s\n', k, mat2str(M,3))
end
k = 2, M = [4 6 7 2.5 -1.5 -2.5 -2 1 3.5 4.5] k = 3, M = [6 6 4.33 1 -2 -2 -0.333 2 4 4.5] k = 4, M = [6 6 4.25 2.75 0 -1.75 -0.75 0.75 2.75 4]
  댓글 수: 1
Angel Lozada
Angel Lozada 2024년 10월 7일
Dear Bruno.
Thanks for your support and cooperation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Measurements and Statistics에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by