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
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
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)
% or reshape back
reshape(movavg(A(:), "custom", B), size(A))
movavg(A, "custom", B) % error
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.
댓글 수: 0
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')
% or use movmean
C = movmean(A, 2)
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.
댓글 수: 0
Sulaymon Eshkabilov
2023년 1월 29일
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)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!