[R] Looking for a faster code

RAVI VARADHAN rvaradhan at jhmi.edu
Wed Jan 31 13:41:39 CET 2007


I have two matrices A (m x 2) and B (n x 2), where m and n are large integers (on the order of 10^4).  I am looking for an efficient way to create another matrix, W (m x n), which can be defined as follows:

for (i in 1:m){
for (j in 1:n) {
W[i,j] <- g(A[i,], B[j,])
} }

I have tried the following and it works okay, but I am sure that I can do even better:

for (i in 1:m) {
W[i,] <- apply(B, 1, y=A[i,], function(x,y) g(y,x))

How can I do this in a faster manner (for example, I feel that I should be able to use "outer")?  

Thanks for any suggestions.


More information about the R-help mailing list