Filter löschen
Filter löschen

Calculation of the moving mean for the first items

48 Ansichten (letzte 30 Tage)
Angel Lozada
Angel Lozada am 5 Okt. 2024 um 16:07
Verschoben: Image Analyst vor etwa 5 Stunden
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.

Antworten (3)

Torsten
Torsten vor etwa 24 Stunden
Verschoben: Image Analyst vor etwa eine Stunde
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
Bruno Luong am 5 Okt. 2024 um 16:48
Bearbeitet: Bruno Luong vor etwa 11 Stunden
"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]

Venkat Siddarth Reddy
Venkat Siddarth Reddy vor etwa 23 Stunden
Bearbeitet: Venkat Siddarth Reddy vor etwa 23 Stunden
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".

Kategorien

Mehr zu Data Preprocessing finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by