Re: [R] efficient way to fill up matrix (and evaluate function)
For the example of the function you gave, it is already 'vectorized': myfunc - function(x1, x2) { + x1 + x2 + } myfunc(1:10, 1:10) [1] 2 4 6 8 10 12 14 16 18 20 outer(1:10, 1:10, myfunc) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]23456789 1011 [2,]3456789 10 1112 [3,]456789 10 11 1213 [4,]56789 10 11 12 1314 [5,]6789 10 11 12 13 1415 [6,]789 10 11 12 13 14 1516 [7,]89 10 11 12 13 14 15 1617 [8,]9 10 11 12 13 14 15 16 1718 [9,] 10 11 12 13 14 15 16 17 1819 [10,] 11 12 13 14 15 16 17 18 1920 Notice it can take a vector of the two parameters and compute the sum. 'outer' also works. So this is your example. On Mon, Nov 28, 2011 at 12:10 AM, Joshua Wiley jwiley.ps...@gmail.com wrote: Here is an example, of course, this is predicated on how myfunc() behaves---if it could not handle adding a constant to a vector, things would choke: ## Current method myfunc - function(x1, x2) { x1 + x2 } x - 1:10 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]) } } A ## partially vectorized A2 - matrix(0, nrow = n, ncol = n) for (i in 1:n){ A2[i, ] - myfunc(x[i], x) } A2 ## even more so A3 - myfunc(outer(rep(1, length(x)), x), x) all.equal(A, A2, A3) Cheers, Josh On Sun, Nov 27, 2011 at 8:54 PM, Sachinthaka Abeywardana sachin.abeyward...@gmail.com wrote: Hi Jim, What exactly do you mean by vectorized. I think outer looks like what I was looking for. BUT there was a (weighted) distance matrix calculation that I was trying to vectorize, which wasnt related to this post. Could you proved a bit more details as to what you were referring to, and maybe an example as how to vectorize in R? Thanks, Sachin On Mon, Nov 28, 2011 at 3:25 PM, jim holtman jholt...@gmail.com wrote: Take a look at 'outer' and vectorized your function. Also look at 'expand.grid'. On Sunday, November 27, 2011, Sachinthaka Abeywardana sachin.abeyward...@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 guide http://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]] __ R-help@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/ __ R-help@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. -- 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. __ R-help@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.
[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]] __ R-help@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.
Re: [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.abeyward...@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 guide http://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]] __ R-help@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.
Re: [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.abeyward...@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 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/ __ R-help@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.
Re: [R] efficient way to fill up matrix (and evaluate function)
Hi Jim, What exactly do you mean by vectorized. I think outer looks like what I was looking for. BUT there was a (weighted) distance matrix calculation that I was trying to vectorize, which wasnt related to this post. Could you proved a bit more details as to what you were referring to, and maybe an example as how to vectorize in R? Thanks, Sachin On Mon, Nov 28, 2011 at 3:25 PM, jim holtman jholt...@gmail.com wrote: Take a look at 'outer' and vectorized your function. Also look at 'expand.grid'. On Sunday, November 27, 2011, Sachinthaka Abeywardana sachin.abeyward...@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 guide http://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]] __ R-help@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.
Re: [R] efficient way to fill up matrix (and evaluate function)
Here is an example, of course, this is predicated on how myfunc() behaves---if it could not handle adding a constant to a vector, things would choke: ## Current method myfunc - function(x1, x2) { x1 + x2 } x - 1:10 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]) } } A ## partially vectorized A2 - matrix(0, nrow = n, ncol = n) for (i in 1:n){ A2[i, ] - myfunc(x[i], x) } A2 ## even more so A3 - myfunc(outer(rep(1, length(x)), x), x) all.equal(A, A2, A3) Cheers, Josh On Sun, Nov 27, 2011 at 8:54 PM, Sachinthaka Abeywardana sachin.abeyward...@gmail.com wrote: Hi Jim, What exactly do you mean by vectorized. I think outer looks like what I was looking for. BUT there was a (weighted) distance matrix calculation that I was trying to vectorize, which wasnt related to this post. Could you proved a bit more details as to what you were referring to, and maybe an example as how to vectorize in R? Thanks, Sachin On Mon, Nov 28, 2011 at 3:25 PM, jim holtman jholt...@gmail.com wrote: Take a look at 'outer' and vectorized your function. Also look at 'expand.grid'. On Sunday, November 27, 2011, Sachinthaka Abeywardana sachin.abeyward...@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 guide http://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]] __ R-help@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/ __ R-help@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.