Martin
Martin

Reputation: 195

R: Extract values from matrix relative to row/column position

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.

Upvotes: 3

Views: 1409

Answers (3)

MichaelChirico
MichaelChirico

Reputation: 34703

The row and column functions make this way simpler than the other solutions, if I understand correctly:

f <- function(k, m) sum(m[row(m) + col(m) >= k])

For your m.ex:

sapply(c(2, 4, 5), f, m = m.ex)
# [1] 45 38 23

For larger examples:

set.seed(1230)
n <- 8
> print(round(m <- matrix(runif(n^2), nrow = n), 2))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0.57 0.87 0.94 0.98 0.87 0.66 0.16 0.98
[2,] 0.65 0.79 0.68 0.74 0.12 0.65 0.56 0.73
[3,] 0.76 0.85 0.71 0.45 0.64 0.45 0.12 0.55
[4,] 0.26 0.09 0.67 0.66 0.58 0.48 0.54 0.20
[5,] 0.38 0.63 0.27 0.16 0.20 0.96 0.05 0.90
[6,] 0.49 0.48 0.71 0.32 0.46 0.98 0.17 0.96
[7,] 0.91 0.99 0.97 0.98 0.84 0.21 0.21 0.44
[8,] 0.62 0.08 0.80 0.88 0.85 0.30 0.61 0.42
> f(12, m)
[1] 8.028652

This can be confirmed by noting the entries you denote are those in the lower-right triangle:

   *    *    *    *    *    *    *    *
   *    *    *    *    *    *    *    *
   *    *    *    *    *    *    *    *
   *    *    *    *    *    *    * 0.20
   *    *    *    *    *    * 0.05 0.90
   *    *    *    *    * 0.98 0.17 0.96
   *    *    *    * 0.84 0.21 0.21 0.44
   *    *    * 0.88 0.85 0.30 0.61 0.42

So the sum is 0.88+0.84+0.85+0.98+0.21+0.3+0.05+0.17+0.21+0.61+0.2+0.9+0.96+0.44+0.42 which is about 8.03.

Upvotes: 4

Heroka
Heroka

Reputation: 13139

Without loops, hope it's useful.

library(reshape2)

#easy way to get all row and column indexes is to transform matrix to long
#has advantage of allowing vectorized computation and avoiding for-loops
myfun <- function(k, mm){
  #reshape matrix to easily get column and row numbers
  melt_m <- melt(mm, varnames=c("row","col"))
  #add row and col indixes
  melt_m$sum_row_col <- melt_m$row + melt_m$col
  #calculate result and return (sum of value when sum of rowcol>=k)
  return(sum(melt_m$value[melt_m$sum_row_col>=k]))
}

#example 1
test_m <- matrix(1:9,3,3,byrow=T)


> myfun(k=2,mm=test_m)
[1] 45
> myfun(k=4, mm=test_m)
[1] 38

Example of what melt does with a matrix:

> test_m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
> melt(test_m,varnames=c("row","col"))
  row col value
1   1   1     1
2   2   1     4
3   3   1     7
4   1   2     2
5   2   2     5
6   3   2     8
7   1   3     3
8   2   3     6
9   3   3     9

Upvotes: 4

Yuri Robbers
Yuri Robbers

Reputation: 291

Well, it's slow and it's ugly, and I'm sure many people will come up with better, faster and more beautiful solutions, but this will do the trick for you:

weakly_exceeds_sum <- function(m, k){
    tmp <- NULL
    for(i in 1:nrow(m)){
        for(j in 1:nrow(m)){
            if(i+j>=k){
            tmp<-c(tmp, m[i,j])
            }
        }
    }
    sum(tmp)
}

where you'd call the function with, for example: weakly_exceeds_sum(m.ex, 2)

Upvotes: 2

Related Questions