useR's, I am trying trying to find out if there is a faster way to do a certain computation. I have successfully used FOR loops and the apply function to do this, but it can take some time to fully compute, but I was wondering if anyone may know of a different function or way to do this:> x[1] 1 2 3 4 5> xk[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. It should result in a 13 by 5 matrix or data frame. Does anyone have a "quicker" solution than FOR loops or apply()? Much appreciation and thanks, dxc13 -- View this message in context: http://www.nabble.com/finding-a-faster-way-to-do-an-iterative-computation-tp18718233p18718233.html Sent from the R help mailing list archive at Nabble.com.
On 30/07/2008, at 6:12 AM, dxc13 wrote:> > useR's, > > I am trying trying to find out if there is a faster way to do a > certain > computation. I have successfully used FOR loops and the apply > function to > do this, but it can take some time to fully compute, but I was > wondering if > anyone may know of a different function or way to do this: >> x > [1] 1 2 3 4 5 >> xk > [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 > > I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. It should > result > in a 13 by 5 matrix or data frame. Does anyone have a "quicker" > solution > than FOR loops or apply()?outer(xk,x,function(a,b){abs(a-b)}) ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Christos Hatzis
2008-Jul-29 20:55 UTC
[R] finding a faster way to do an iterative computation
matrix(rep(x, each=13) - xk, nrow=13)> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of dxc13 > Sent: Tuesday, July 29, 2008 2:13 PM > To: r-help at r-project.org > Subject: [R] finding a faster way to do an iterative computation > > > useR's, > > I am trying trying to find out if there is a faster way to do > a certain computation. I have successfully used FOR loops > and the apply function to do this, but it can take some time > to fully compute, but I was wondering if anyone may know of a > different function or way to do this: > > x > [1] 1 2 3 4 5 > > xk > [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 > > I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. It > should result in a 13 by 5 matrix or data frame. Does anyone > have a "quicker" solution than FOR loops or apply()? > > Much appreciation and thanks, > dxc13 > -- > View this message in context: > http://www.nabble.com/finding-a-faster-way-to-do-an-iterative- > computation-tp18718233p18718233.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > >
Moshe Olshansky
2008-Jul-29 22:21 UTC
[R] finding a faster way to do an iterative computation
Try abs(outer(xk,x,"-")) (see ?outer) --- On Wed, 30/7/08, dxc13 <dxc13 at health.state.ny.us> wrote:> From: dxc13 <dxc13 at health.state.ny.us> > Subject: [R] finding a faster way to do an iterative computation > To: r-help at r-project.org > Received: Wednesday, 30 July, 2008, 4:12 AM > useR's, > > I am trying trying to find out if there is a faster way to > do a certain > computation. I have successfully used FOR loops and the > apply function to > do this, but it can take some time to fully compute, but I > was wondering if > anyone may know of a different function or way to do this: > > x > [1] 1 2 3 4 5 > > xk > [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 > > I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. > It should result > in a 13 by 5 matrix or data frame. Does anyone have a > "quicker" solution > than FOR loops or apply()? > > Much appreciation and thanks, > dxc13 > -- > View this message in context: > http://www.nabble.com/finding-a-faster-way-to-do-an-iterative-computation-tp18718233p18718233.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.