[R] efficient way to fill up matrix (and evaluate function)

Joshua Wiley jwiley.psych at gmail.com
Mon Nov 28 05:25:57 CET 2011


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/



More information about the R-help mailing list