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. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. 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 Email: rvaradhan@jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- [[alternative HTML version deleted]]
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. > > > > > > > > ---------------------------------------------------------------------- > ------ > ------- > > Ravi Varadhan, Ph.D. > > 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 > > Email: rvaradhan at jhmi.edu > > Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/ > Varadhan.html > > > > ---------------------------------------------------------------------- > ------ > -------- > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- 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}}
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 Address: Kapucijnenvoer 35, Leuven, Belgium 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 ----- From: "Ravi Varadhan" <rvaradhan at jhmi.edu> 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. > > > > > > > > ---------------------------------------------------------------------------- > ------- > > Ravi Varadhan, Ph.D. > > 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 > > Email: rvaradhan at jhmi.edu > > Webpage: > http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html > > > > ---------------------------------------------------------------------------- > -------- > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Dear Dimitris, I implemented your solution on my actual problem. I was able to generate my large transition matrix in 56 seconds, compared to the previous time of around 27 minutes. Wow!!! I thank you very much for the help. R and the R-user group are truly amazing! Best regards, Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. 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 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: Dimitris Rizopoulos [mailto:dimitris.rizopoulos at med.kuleuven.be] Sent: Thursday, February 01, 2007 10:33 AM To: Ravi Varadhan Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Need help writing a faster code 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 Address: Kapucijnenvoer 35, Leuven, Belgium 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 ----- From: "Ravi Varadhan" <rvaradhan at jhmi.edu> 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. > > > > > > > >----------------------------------------------------------------------------> ------- > > Ravi Varadhan, Ph.D. > > 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 > > Email: rvaradhan at jhmi.edu > > Webpage: > http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html > > > >----------------------------------------------------------------------------> -------- > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Seemingly Similar Threads
- Imposing Feller condition using project constraint in spg
- finding roots of multivariate equation
- Skipping specified rows in scan or read.table
- How to solve differential equations with a delay (time lag)?
- X-axis labels in histograms drawn by the "truehist" function