Suzan Cioc
Suzan Cioc

Reputation: 30127

How to sum all matrix elements which are lefter and higher?

Suppose I have a 3x4 matrix

enter image description here

Now how to calculate a matrix of the same size, which contains each element being a sum of itself and all elements, which are higher and lefter than it?

enter image description here

Upvotes: 2

Views: 49

Answers (1)

Shai
Shai

Reputation: 114866

You must be looking for cumsum:

integ = cumsum( cumsum( A, 1 ), 2 );

For example:

A = [1 2 3;
     4 5 6];
cumsum( cumsum( A, 1 ), 2 )

Results with:

1    3    6
5   12   21

PS,
This operation is sometimes refereed to as integral image.

Upvotes: 7

Related Questions