user3812887
user3812887

Reputation: 457

Matlab: compute the sum of elements in the lower portion of a matrix

enter image description hereenter image description hereI have to develop a Matlab function that computes the sum of the elements in the reverse diagonal, and the elements to the right of the reverse diagonal. Sum (A) = 38 ; Sum (B) = 40; Sum (C) = 25;

enter image description hereenter image description here

Can somebody provide me a solution on how to go about using looping to solve this problem ?

Upvotes: 0

Views: 209

Answers (2)

percusse
percusse

Reputation: 3106

For matrix M

Mysum = sum(sum(tril(rot90(M,-1))))

Upvotes: 1

Tymur Gubayev
Tymur Gubayev

Reputation: 500

For a n×m Matrix:

  1. Set the sum to 0, column to 1 and top to n (assuming (n,1) is the bottom-left corner of the matrix)

  2. Add to the sum all elements in current column which row number is more or equal to the top.

  3. Add 1 to column. If column is greater than m we are done.

  4. Subtract 1 from top. If top is 0, set top to 1.

  5. Go to 1.

Upvotes: 1

Related Questions