2D moving sum of a matrix

조회 수: 6 (최근 30일)
Jakob Sievers
Jakob Sievers 2020년 8월 16일
댓글: Jakob Sievers 2020년 8월 16일
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
  댓글 수: 1
KSSV
KSSV 2020년 8월 16일
There is inbuilt function movsum, did you see that?

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
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
Jakob Sievers
Jakob Sievers 2020년 8월 16일
Thank you so much for this. I knew there was a simple way to do this.
I ended up using fspecial to create a circular sum-filter instead of a basic square sum-filter:
filter=fspecial('disk',5)>0;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by