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;

Réponses (2)

Alan Stevens
Alan Stevens le 11 Déc 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
Bruno Luong le 11 Déc 2020
Modifié(e) : Bruno Luong le 11 Déc 2020
A=rand(1000);
[m,n]=size(A);
G=max((1:m)',1:n);
s=cumsum(accumarray(G(:),A(:)))

Catégories

Tags

Question posée :

le 11 Déc 2020

Modifié(e) :

le 11 Déc 2020

Community Treasure Hunt

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

Start Hunting!

Translated by