Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-11 Thread Gregor Gorjanc
Jeffrey J. Hallman frb.gov> writes: > Tim Hesterberg insightful.com> writes: > > > toby_marks americancentury.com asked: > > >I am trying to divide the columns of a matrix by the first row in the > > >matrix. > > > > Dividing columns of a matrix by a vector is a pretty fundamental > > operati

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-09 Thread Jeffrey J. Hallman
Tim Hesterberg <[EMAIL PROTECTED]> writes: > [EMAIL PROTECTED] asked: > >I am trying to divide the columns of a matrix by the first row in the > >matrix. > > Dividing columns of a matrix by a vector is a pretty fundamental > operation, and the query resulted in a large number of suggestions: >

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-07 Thread Tim Hesterberg
[EMAIL PROTECTED] asked: >I am trying to divide the columns of a matrix by the first row in the >matrix. Dividing columns of a matrix by a vector is a pretty fundamental operation, and the query resulted in a large number of suggestions: x/matrix(v, nrow(x), ncol(x), byrow = TRUE)) sweep(x, 2, v

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Prof Brian Ripley
What version of R was this? In 2.4.0 alpha > a <- matrix(1:24,4) > system.time(for(i in 1:1000) junk <- a / rep(a[1,], each = 4)) [1] 0.014 0.000 0.014 0.000 0.000 > system.time(for(i in 1:1000) junk <- t(t(a)/a[1,])) [1] 0.057 0.000 0.058 0.000 0.000 shows a large margin the other way, which in

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Gabor Grothendieck
In terms of speed Toby's original idea was actually the fastest. Here they are decreasing order of the largest timing in each row of system.time. I also tried it with a 100x10 matrix and got almost the same order: > library(reshape) > system.time(for(i in 1:1000) iapply(a, 1, "/", a[1,])) [1] 11.

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread toby_marks
The apply was exactly what I was after. And, I will check out the others as well. great tips! "Gabor Grothendieck" <[EMAIL PROTECTED]> 09/06/2006 11:11 AM To "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> cc r-help@stat.math.ethz.ch Subject Re: [R] Matrix

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Rolf Turner
Prof. Brian Ripley wrote: > On Wed, 6 Sep 2006, Christos Hatzis wrote: > > > See ?sweep > > > > sweep(a, 2, a[1,],"/") > > That is less efficient than > > a/rep(a[1,], each=nrow(a)) *My* first instinct was to use t(t(a)/a[1,]) (which has not heretofore been suggested). This seems t

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Gabor Grothendieck
Yet another one using the idempotent apply in reshape package that eliminates the transpose: library(reshape) iapply(a, 1, "/", a[1,]) On 9/6/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > This last one could also be written slightly shorter as: > > t(apply(a, 1, "/", a[1,])) > > On 9/6/06,

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Gabor Grothendieck
This last one could also be written slightly shorter as: t(apply(a, 1, "/", a[1,])) On 9/6/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > And here is one more: > > t(apply(a, 1, function(x) x/a[1,])) > > On 9/6/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > Here are a few possibilitie

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Prof Brian Ripley
> [EMAIL PROTECTED] > Sent: Wednesday, September 06, 2006 11:49 AM > To: r-help@stat.math.ethz.ch > Subject: [R] Matrix multiplication using apply() or lappy() ? > > I am trying to divide the columns of a matrix by the first row in the > matrix. > > I have tried to get

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Gabor Grothendieck
And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > Here are a few possibilities: > > a <- matrix(1:24, 4) # test data > > a / rep(a[1,], each = 4) > > a / outer(rep(1, nrow(a)), a[1,]) > > a %*% diag(1/a[1,]) > > sweep(a, 2, a[1,],

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Gabor Grothendieck
Here are a few possibilities: a <- matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], "/") On 9/6/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I am trying to divide the columns of a matrix by the first row in th

Re: [R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread Christos Hatzis
See ?sweep sweep(a, 2, a[1,],"/") -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Wednesday, September 06, 2006 11:49 AM To: r-help@stat.math.ethz.ch Subject: [R] Matrix multiplication using apply() or lappy

[R] Matrix multiplication using apply() or lappy() ?

2006-09-06 Thread toby_marks
I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have o