You are pretty close. As described in this related question , all you need is a Vectorize() function to convert your Fun() function to a vectorized version:
VecFun <- Vectorize( Fun )
Then you can simply do:
outer(d.cols, r.cols, VecFun )
eg. if you define
Fun <- function(a,b) sum(a+b)
and r,d defined as follows:
J <- 5 D <- 3 R <- 4 d <- matrix( 1:(J*D), J, D) r <- matrix( 1:(J*R), J, R)
You will get the following:
> outer(d.cols, r.cols, VecFun) 1 2 3 4 1 30 55 80 105 2 55 80 105 130 3 80 105 130 155
source share