Sum of selected elements in Matrix

Hello,
For example, I have a 10x10 matrix. I need to find 1x1, 2x2, 3x3, 4x4 ... matrixes element sum, I couldn't find an answer with the sum command. Could you help.
Example;

Answers (2)

How about:
for i=1:10, S(i) = sum(sum(A(1:i,1:i))); end
where A is your 10x10 matrix and S contains the 10 sums.
Bruno Luong
Bruno Luong on 11 Dec 2020
Edited: Bruno Luong on 11 Dec 2020
A=rand(1000);
[m,n]=size(A);
G=max((1:m)',1:n);
s=cumsum(accumarray(G(:),A(:)))

Categories

Tags

Asked:

on 11 Dec 2020

Edited:

on 11 Dec 2020

Community Treasure Hunt

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

Start Hunting!