如何在 MATLAB 中计算移动和?
向量的移动和可以称为向量中每个大小为 k 的窗口的运行和。
Suppose vector = [1 2 3 4 5]
Then the moving sum of each window of size 2 are
[1 2] = 3
[2 3] = 5
[3 4] = 7
[4 5] = 9
Then vector of moving sums is [3 5 7 9]
Matlab 允许我们使用 movsum()方法有效地执行此操作。与movsum()方法相关联的不同的S yntaxes是:
- M = movsum(A, k)
- M = movsum(A, [kb kf])
- M = movsum(___, 暗淡)
- M = movsum(___, nanflag)
现在让我们详细讨论上述语法:
movsum(A, k)
- 该方法返回向量 A 的 k 大小窗口总和数组。
- 返回的向量将具有与 A 相同的大小。
- 如果 k 是奇数,则窗口以当前位置的元素为中心。
Suppose A = [1 2 3 4 5] and k = 3, then sliding window size is 3.
Since k is odd, window is centered at every index and considers one value from left and one value from right into a window.
Then windows are
[1 2] –> Since at first index(i.e value 1) there is no left value, then window gets truncated to size 2
[1 2 3]
[2 3 4]
[3 4 5]
[4 5] —> Since at last index(i.e value 5) there is no right value, then window gets truncated to size 2
- 如果k 是偶数,则窗口以当前元素和前一个元素为中心。
- 当窗口中的元素很少时,窗口会自动被截断并考虑被截断窗口的总和。
Matlab
% Input vector
A = [1 2 3 4 5];
disp("Vector :");
disp(A);
% Moving sum of A of size 3
M = movsum(A,3);
disp("Moving sum :");
disp(M);
Matlab
% Input vector
A = [1 2 3 4 5];
disp("Vector :");
disp(A);
% Moving sum of A of size 3
% every window having 2 elements
% to left and 0 elements to the
% right
M = movsum(A,[2 0]);
disp("Moving sum :");
disp(M);
Matlab
% Input vector
A = [1 2 3; 4 5 6; 7 8 9];
disp("Matrix :");
disp(A);
% Moving sum of A of size k=3
% along each row(dim=1)
M = movsum(A,3,2);
disp("Moving sum :");
disp(M);
Matlab
% Input vector
A = [3 5 NaN 9 0 NaN];
disp("Vector :");
disp(A);
% Including NaN values
B = movsum(A,3,'includenan');
disp("Moving sum Incude NaN :");
disp(B);
% Excluding NaN values
B = movsum(A,3,'omitnan');
disp("Moving sum Exclude NaN :");
disp(B);
输出 :
movsum(A, [kb kf])
返回向量 A 的移动总和,每个窗口都以当前位置为中心,左侧kb元素和右侧kf元素的总窗口大小为kb+kf+1 。
Suppose A = [1 2 3 4 5] and [kb = 2 kf = 0], then sliding window size is kb+kf+1 = 3
At every element previous 2 elements and next 0 elements are considered into the window along with current element.
Then windows are
[1]
[1 2]
[1 2 3]
[2 3 4]
[3 4 5]
MATLAB
% Input vector
A = [1 2 3 4 5];
disp("Vector :");
disp(A);
% Moving sum of A of size 3
% every window having 2 elements
% to left and 0 elements to the
% right
M = movsum(A,[2 0]);
disp("Moving sum :");
disp(M);
输出 :
movsum(___,dim)
- 通过计算 dim 的每个维度返回矩阵的移动总和。
- dim取两个值1 或 2 分别对应于沿每一列和每一行的操作。
- dim = 1的默认值,即在每列上执行矩阵的移动总和。
MATLAB
% Input vector
A = [1 2 3; 4 5 6; 7 8 9];
disp("Matrix :");
disp(A);
% Moving sum of A of size k=3
% along each row(dim=1)
M = movsum(A,3,2);
disp("Moving sum :");
disp(M);
输出 :
movsum(___,nanflag)
- nanflag值决定在移动求和中是否包含或排除向量的 NaN 值。
- nanflag取两个值'includenan' 或 'omitnan' 分别对应于包含 NaN 元素和排除 NaN 元素。
- omitNaN'将 NaN 值视为0 。
注意: NaN + 数字 = NaN
MATLAB
% Input vector
A = [3 5 NaN 9 0 NaN];
disp("Vector :");
disp(A);
% Including NaN values
B = movsum(A,3,'includenan');
disp("Moving sum Incude NaN :");
disp(B);
% Excluding NaN values
B = movsum(A,3,'omitnan');
disp("Moving sum Exclude NaN :");
disp(B);
输出 :