Calculation of the moving mean for the first items
8 ビュー (過去 30 日間)
古いコメントを表示
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)
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)
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)
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.
0 件のコメント
採用された回答
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".
その他の回答 (2 件)
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.
Bruno Luong
2024 年 10 月 5 日
編集済み: Bruno Luong
2024 年 10 月 6 日
From movmean doc
"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]
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
参考
カテゴリ
Help Center および File Exchange で Measurements and Statistics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!