My problem is as follows: Suppose we have a quadratic n*n matrix, e.g.
m <- matrix(runif(n^2), n,n)
Now I want to define a function f=function(k)
that returns the sum of all matrix entries for which the sum of their row and column number weakly exceeds k. For example, consider the 3*3 matrix
m.ex <- matrix(1:9, 3,3, byrow = T)
which looks like
1 2 3
4 5 6
7 8 9
Then f(2) should give 45 = 1+2+3+4+5+6+7+8+9 (as for every entry in the matrix, the sum of the row and column position weakly exceeds 2), f(4) = 38 = 3+5+6+7+8+9 (as the sum of the row and column position weakly exceeds 4 for positions (1,3), (2,2), (2,3), (3,1), (3,2), and (3,3)), and f(5) = 23 = 6 + 8 + 9 (as the sum of the row and columin position weakly exceeds 5 for positions (2,3), (3,2), and (3,3)). Etc.