[R] Looking for a faster code

From: RAVI VARADHAN <rvaradhan_at_jhmi.edu>
Date: Wed 31 Jan 2007 - 12:41:39 GMT


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.


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Jan 31 23:57:05 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 31 Jan 2007 - 13:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.