movavg with custom type and weights

15 次查看(过去 30 天)
I have difficulties using the movavg with custom type and weight. The minimum reproducible code is given below
It complained that the weight vector is not an array with all values <= 1 but isn't B clearly fulfil this criteria? Or am I missing something?
  1 个评论
Jan
Jan 2023-1-29
Please post code as formatted text, not as screenshot. Then it is much easier to re-use it by copy&paste.

请先登录,再进行评论。

回答(3 个)

Bruno Luong
Bruno Luong 2023-1-29
编辑:Bruno Luong 2023-1-29
movavg requires first array is a column vector or matrix
A = 1:5;
B = [0.5 0.5];
movavg(A(:), "custom", B)
ans = 5×1
1.0000 1.5000 2.5000 3.5000 4.5000
% or reshape back
reshape(movavg(A(:), "custom", B), size(A))
ans = 1×5
1.0000 1.5000 2.5000 3.5000 4.5000
movavg(A, "custom", B) % error
Error using movavg
Expected weights vector to be an array with all of the values <= 1.
The error message is for sure misleading.
Furthermore according to the doc
"To compute moving average with custom weights, the weights (w) are first normalized such that they sum to one:
W(i) = w(i)/sum(w), for i = 1,2,...,N"
Not serious TMW on document-error handling this function.

Image Analyst
Image Analyst 2023-1-29
编辑:Image Analyst 2023-1-29
I never heard of movavg - I guess it's only in the Financial Toolbox. You can use conv or movmean instead:
A = [1,2,3,4,5]; % Data to be filtered
B = [0.5, 0.5]; % Weights
C = conv(A, B, 'valid')
C = 1×4
1.5000 2.5000 3.5000 4.5000
% or use movmean
C = movmean(A, 2)
C = 1×5
1.0000 1.5000 2.5000 3.5000 4.5000
Depending on what you want your output to be (how you want edge efects to be handled). Conv() works with any weights. Movmean only handles uniform weights as far as I know.

Sulaymon Eshkabilov
In this case, it is better to use filter() that gives a good moving average filter solution:
A = 1:5;
B = [.5 .5 ];
Out_A = filter(B, 1, A)
Out_A = 1×5
0.5000 1.5000 2.5000 3.5000 4.5000

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by