2D moving sum of a matrix
9 次查看(过去 30 天)
显示 更早的评论
Hi there
How do I perform a moving sum within a matrix? I.e.: the result of each index will be the sum of all surrounding indexes, as defined by window lengths (with some edge effects, as with a regular moving sum).
I could definitely set up some basic loop function but I anticipate having to do this quite often and so I wish to find a smarter/faster solution. And I suspect that there definitely is a smarter solution. Like maybe using filter2 or something?
Thanks in advance for all help!
Cheers
Jakob
采纳的回答
Bruno Luong
2020-8-16
编辑:Bruno Luong
2020-8-16
Assuming A is your array, w is the window size
% testexample
A=randi(10,5)
movsum(movsum(A,w,1),w,2)
% or
conv2(A, ones(w), 'same')
% or
filter2(ones(w), A, 'same')
更多回答(1 个)
Walter Roberson
2020-8-16
conv2(YourMatrix, ones(WindowHeight, WindowWidth))
You might want to add options such as 'valid' to control the size of output and the edge effects.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!