Sum of selected elements in Matrix
Show older comments
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)
Alan Stevens
on 11 Dec 2020
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
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
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!