Consider any matrix, say Rain=(1:10)'. I want to compute a non-overlapping moving sum with two element.
Rain=1,2,3,4,5,6,7,8,9,10
Compute=1+2,3+4,5+6,7+8,9+10
With traditional movsum command, the moving sum is overlapping elements. I want to evaluate non-overlapping moving sum. Any in-built function? I have tried with loops and all those, it works but in-built function would be fast to compute.

 채택된 답변

Star Strider
Star Strider 2021년 6월 27일

2 개 추천

Try this —
Rain = [1,2,3,4,5,6,7,8,9,10];
rRain = reshape(Rain, 2, [])
rRain = 2×5
1 3 5 7 9 2 4 6 8 10
sumRain = sum(rRain)
sumRain = 1×5
3 7 11 15 19
These could be combined into one line, however I kept them separate to demonstrate how it works.
.

댓글 수: 6

Suman Dhamala
Suman Dhamala 2021년 6월 27일
Actually my matrix has size 135*129*118. I wanted to accomplish this non-overlapping 2 sum so that my final matrix is 135*129*59. I tried using reshape, but may be i did some mistake the resultant matrix wasn't the one i wanted, could you provide some hint for 3-d matrix?
Star Strider
Star Strider 2021년 6월 27일
It always helps to have the actual problem in the beginning.
Every combination of permute, reshape and sum that I experimented with failed to provide the desired result.
.
Image Analyst
Image Analyst 2021년 6월 27일
Star's right - avoids wasting time if we know the full situation in advance. However he showed you a nice trick that will come in useful. It's also in the FAQ:
Matt J
Matt J 2021년 6월 27일
편집: Matt J 2021년 6월 27일
You can download sepblockfun, which generalizes the technique Star Strider has shown to higher dimensions.
A=rand(135,129,118);
B=sepblockfun(A,[1,1,2],'sum');
whos A B
Name Size Bytes Class Attributes A 135x129x118 16439760 double B 135x129x59 8219880 double
Star Strider
Star Strider 2021년 6월 27일
@Matt J — Thank you! I didn’t think to look in FEX.
Suman Dhamala
Suman Dhamala 2021년 6월 27일
This worked thanks @Matt J and everyone

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 6월 27일

0 개 추천

Since you have an image, you can do it with blockproc. However it only works with 2-D arrays so you'll have to do it once on each slice, then again along the z direction. I'm attaching some blockproc demos. I haven't done it with a 3-D image so you're on your own but I'm pretty sure it can be done.

제품

릴리스

R2019a

태그

질문:

2021년 6월 27일

댓글:

2021년 6월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by