# [R] Need help writing a faster code

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Feb 1 16:33:18 CET 2007

```the following seems to be a first improvement:

m <- 2000
n <- 5000
A <- matrix(rnorm(2*m), ncol=2)
B <- matrix(rnorm(2*n), ncol=2)
W1 <- W2 <- matrix(0, m, n)

##############################
##############################

g1 <- function(x, y){
theta <- atan((y[2] - x[2]) / (y[1] - x[1]))
theta + 2*pi*(theta < 0)
}

invisible({gc(); gc()})
system.time(for (i in 1:m) {
W1[i, ] <- apply(B, 1, y = A[i,], function(x, y) g1(y, x))
})

##############################

g2 <- function(x){
out <- tB - x
theta <- atan(out[2, ] / out[1, ])
theta + 2*pi*(theta < 0)
}

tB <- t(B)
invisible({gc(); gc()})
system.time(for (i in 1:m) {
W2[i, ] <- g2(A[i, ])
})

## or

invisible({gc(); gc()})
system.time(W3 <- t(apply(A, 1, g2)))

all.equal(W1, W2)
all.equal(W1, W3)

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, February 01, 2007 4:10 PM
Subject: [R] Need help writing a faster code

> Hi,
>
>
>
> I apologize for this repeat posting, which I first posted yesterday.
> I would
> appreciate any hints on solving this problem:
>
>
>
> 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,])
>
> } }
>
> where g(x,y) is a function that takes two vectors and returns a
> scalar.
>
>
>
> The following works okay, but is not fast enough for my purpose.  I
> am sure
> that I can do 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? I attempted "outer",
> "kronecker",
> "expand.grid", etc, but with no success.
>
>
>
> Here is an example:
>
>
>
> m <- 2000
>
> n <- 5000
>
> A <- matrix(rnorm(2*m),ncol=2)
>
> B <- matrix(rnorm(2*n),ncol=2)
>
> W <- matrix(NA, m, n)
>
>
>
> for (i in 1:m) {
>
> W[i,] <- apply(B, 1, y=A[i,], function(x,y) g(y,x))
>
> }
>
>
>
> g <- function(x,y){
>
> theta <- atan((y[2]-x[2]) / (y[1] - x[1]))
>
> theta + 2*pi*(theta < 0)
>
> }
>
>
>
> Thanks for any suggestions.
>
>
>
> Best,
>
> Ravi.
>
>
>
>
>
>
>
> ----------------------------------------------------------------------------
> -------
>
>
> Assistant Professor, The Center on Aging and Health
>
> Division of Geriatric Medicine and Gerontology
>
> Johns Hopkins University
>
> Ph: (410) 502-2619
>
> Fax: (410) 614-9625
>
>
> Webpage:
>
>
>
> ----------------------------------------------------------------------------
> --------
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help