Sachinthaka Abeywardana
2011-Nov-28 04:16 UTC
[R] efficient way to fill up matrix (and evaluate function)
Hi All, I want to do something along the lines of: for (i in 1:n){ for (j in 1:n){ A[i,j]<-myfunc(x[i], x[j]) } } The question is what would be the most efficient way of doing this. Would using functions such as sapply be more efficient that using a for loop? Note that n can be a few thousand. Thus atleast a 1000x1000 matrix. Thanks, Sachin [[alternative HTML version deleted]]
jim holtman
2011-Nov-28 04:25 UTC
[R] efficient way to fill up matrix (and evaluate function)
Take a look at 'outer' and vectorized your function. Also look at 'expand.grid'. On Sunday, November 27, 2011, Sachinthaka Abeywardana < sachin.abeywardana@gmail.com> wrote:> Hi All, > > I want to do something along the lines of: > for (i in 1:n){ > for (j in 1:n){ > A[i,j]<-myfunc(x[i], x[j]) > } > } > > The question is what would be the most efficient way of doing this. Would > using functions such as sapply be more efficient that using a for loop? > > Note that n can be a few thousand. Thus atleast a 1000x1000 matrix. > > Thanks, > Sachin > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
Joshua Wiley
2011-Nov-28 04:25 UTC
[R] efficient way to fill up matrix (and evaluate function)
Hi Sachin, The technique you are suggesting is likely to be just as efficient as any other if indeed myfunc must be called on each x[i] x[j] element individually. I would take the additional steps of instantiating A as a matrix (something like: A <- matrix(0, nrow = n, ncol = n) Depending, you may also squeeze a bit more performance out by doing something like: f <- function(x) { n <- length(x) A <- matrix(0, nrow = n, ncol = n) for (i in 1:n){ for (j in 1:n){ A[i,j]<-myfunc(x[i], x[j]) } return(A) } require(compiler) f <- cmpfun(f) A <- f(x) However, any big performance increases (if possible) are likely to come via vectorizing myfunc so that it can operate on say, myfunc(x[1], x). If you post the code to myfunc, we may be able to help improve its performance or rework it so that it can handle x as a vector rather than element by element. Cheers, Josh On Sun, Nov 27, 2011 at 8:16 PM, Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> wrote:> Hi All, > > I want to do something along the lines of: > for (i in 1:n){ > ? ?for (j in 1:n){ > ? ? ? ?A[i,j]<-myfunc(x[i], x[j]) > ? ?} > } > > The question is what would be the most efficient way of doing this. Would > using functions such as sapply be more efficient that using a for loop? > > Note that n can be a few thousand. Thus atleast a 1000x1000 matrix. > > Thanks, > Sachin > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/