MATLAB: Given an mxn matrix, we’ve have to find the sum of elements for which the sum of their row no.(x) and column no.(y) is greater than m(total no. of rows)

find elements for which x+y > r ( x - row no.find elements for which x+y > r ( x = row no.) ( y = col no.)(r = total no. of rows)

given an mxn matrix – A, we've have to find the sum of elements for which the sum of their row no.(x) and column no.(y) is greater than m(total no. of rows) . how to extract the elements like a((x1,y1),(x2,y2) …) where x1 and y1 correspond to the desired coordinates as pr the condition. it will be better if no loop is used thanks

Best Answer

out = sum(A(flip(triu(true(size(A))))))
or for MATLAB >= R2016b
[m,n] = size(A);
out = sum(A((1:m)' + (1:n) > m))
or MATLAB <= R2016a
out1 = sum(A(bsxfun(@plus,(1:m)',1:n) >m))
Related Question