# [R] Need help writing a faster code

Robin Hankin r.hankin at noc.soton.ac.uk
Thu Feb 1 16:27:16 CET 2007

```Hi

> A <- matrix(runif(10),ncol=2)
> B <- matrix(runif(10),ncol=2)
> g <- function(i4){theta <- atan2( (i4[4]-i4[2]),(i4[3]-i4[1]))
+ return(theta + 2*pi*(theta<0))}
> apply(A,1,function(x){apply(B,1,function(y){g(c(x,y))})})
[,1]      [,2]     [,3]        [,4]      [,5]
[1,] 1.1709326 2.6521457 3.857477 0.219274562 1.2948374
[2,] 1.1770919 4.2109056 4.057313 4.918552967 1.9733967
[3,] 0.9171661 0.6721475 4.193675 0.434253839 0.9781060
[4,] 0.9181475 0.6911804 4.213295 0.455127422 0.9771797
[5,] 1.0467449 4.9263243 3.983248 0.004371504 1.1693707
>

HTH

rksh

On 1 Feb 2007, at 15:10, Ravi Varadhan wrote:

> 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:  http://www.jhsph.edu/agingandhealth/People/Faculty/
>
>
>
> ----------------------------------------------------------------------
> ------
> --------
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel  023-8059-7743

--
This e-mail (and any attachments) is confidential and intend...{{dropped}}

```