T_D
T_D

Reputation: 67

efficient vectorizisation of a double for loop

how can one vectorize the following double for loop in R?

a <-  seq(1,10, length=5)
b <- seq(0,1, length=4)
fun <- function(a,b){return(a+b)}

out <- matrix(NaN, nrow=5, ncol=4)

  for(i in 1:5) {
    for(j in 1:4) {
      out[i, j] <- fun(a[i], b[j])
    }
  }

I have attempted, for example, without success. Please advise, thanks in advance

outer(1:nrow(out), 1:ncol(out), FUN = fun(a,b))
mapply(out, fun)

Upvotes: 2

Views: 111

Answers (2)

mrip
mrip

Reputation: 15163

As a general rule, vectorizing the innermost loop is usually enough to get you the performance gains. In this case that would mean:

for(i in 1:5) {
  out[i,]<- a[i] + b
}

Assuming that a and b both have length O(n), then by doing it this way, you ensure that the overhead from iterating through a loop in R is only incurred O(n) times, rather than O(n^2) times if you do both loops in R. Since the running time of the algorithm is going to be O(n^2) anyway, the additional O(n) overhead won't affect the running time much.

Of course, for this particular example, you could use outer like others have suggested.

Upvotes: 2

Tyler Rinker
Tyler Rinker

Reputation: 109874

What about:

outer(a, b, '+')

## > outer(a, b, '+')
##       [,1]      [,2]      [,3]  [,4]
## [1,]  1.00  1.333333  1.666667  2.00
## [2,]  3.25  3.583333  3.916667  4.25
## [3,]  5.50  5.833333  6.166667  6.50
## [4,]  7.75  8.083333  8.416667  8.75
## [5,] 10.00 10.333333 10.666667 11.00

Upvotes: 3

Related Questions