toby_marks at americancentury.com
2006-Sep-06 15:48 UTC
[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 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 observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3] [,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 ------------------------------------------------------------ CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}}
Christos Hatzis
2006-Sep-06 15:56 UTC
[R] Matrix multiplication using apply() or lappy() ?
See ?sweep sweep(a, 2, a[1,],"/") -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of toby_marks at americancentury.com Sent: Wednesday, September 06, 2006 11:49 AM To: r-help at 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 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 observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3] [,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 ------------------------------------------------------------ CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} ______________________________________________ R-help at stat.math.ethz.ch 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.
Gabor Grothendieck
2006-Sep-06 16:03 UTC
[R] Matrix multiplication using apply() or lappy() ?
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, toby_marks at americancentury.com <toby_marks at americancentury.com> wrote:> 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 observed examples in the archives using this type of approach. Does > anybody have a snippet of a call to apply() that would accomplish this as > well? > > Thanks! > > > seed=50 > $a = array(rnorm(20),dim=c(4,5)) > $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) > $a > [,1] [,2] [,3] [,4] [,5] > [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 > [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 > [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 > [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 > > $a/b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 > [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 > [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 > [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 > > > > ------------------------------------------------------------ > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Gabor Grothendieck
2006-Sep-06 16:08 UTC
[R] Matrix multiplication using apply() or lappy() ?
And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck <ggrothendieck at gmail.com> 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,], "/") > > > On 9/6/06, toby_marks at americancentury.com > <toby_marks at americancentury.com> wrote: > > 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 observed examples in the archives using this type of approach. Does > > anybody have a snippet of a call to apply() that would accomplish this as > > well? > > > > Thanks! > > > > > > seed=50 > > $a = array(rnorm(20),dim=c(4,5)) > > $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) > > $a > > [,1] [,2] [,3] [,4] [,5] > > [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 > > [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 > > [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 > > [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 > > > > $a/b > > [,1] [,2] [,3] [,4] [,5] > > [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 > > [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 > > [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 > > [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 > > > > > > > > ------------------------------------------------------------ > > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > >
Prof Brian Ripley
2006-Sep-06 16:10 UTC
[R] Matrix multiplication using apply() or lappy() ?
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))> > -Christos > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > toby_marks at americancentury.com > Sent: Wednesday, September 06, 2006 11:49 AM > To: r-help at 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 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 observed examples in the archives using this type of approach. Does > anybody have a snippet of a call to apply() that would accomplish this as > well? > > Thanks! > > > seed=50 > $a = array(rnorm(20),dim=c(4,5)) > $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) > $a > [,1] [,2] [,3] [,4] [,5] > [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 > 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 > 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 > 0.28488295 1.3997368 > > $a/b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 [2,] -0.3163225 > -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 > 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 > 0.4193841 -3.811560 > > > > ------------------------------------------------------------ > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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-help at stat.math.ethz.ch 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Gabor Grothendieck
2006-Sep-06 16:11 UTC
[R] Matrix multiplication using apply() or lappy() ?
This last one could also be written slightly shorter as: t(apply(a, 1, "/", a[1,])) On 9/6/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> And here is one more: > > t(apply(a, 1, function(x) x/a[1,])) > > On 9/6/06, Gabor Grothendieck <ggrothendieck at gmail.com> 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,], "/") > > > > > > On 9/6/06, toby_marks at americancentury.com > > <toby_marks at americancentury.com> wrote: > > > 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 observed examples in the archives using this type of approach. Does > > > anybody have a snippet of a call to apply() that would accomplish this as > > > well? > > > > > > Thanks! > > > > > > > > > seed=50 > > > $a = array(rnorm(20),dim=c(4,5)) > > > $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) > > > $a > > > [,1] [,2] [,3] [,4] [,5] > > > [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 > > > [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 > > > [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 > > > [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 > > > > > > $a/b > > > [,1] [,2] [,3] [,4] [,5] > > > [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 > > > [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 > > > [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 > > > [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 > > > > > > > > > > > > ------------------------------------------------------------ > > > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch 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. > > > > > >
Gabor Grothendieck
2006-Sep-06 16:19 UTC
[R] Matrix multiplication using apply() or lappy() ?
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 <ggrothendieck at gmail.com> wrote:> This last one could also be written slightly shorter as: > > t(apply(a, 1, "/", a[1,])) > > On 9/6/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: > > And here is one more: > > > > t(apply(a, 1, function(x) x/a[1,])) > > > > On 9/6/06, Gabor Grothendieck <ggrothendieck at gmail.com> 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,], "/") > > > > > > > > > On 9/6/06, toby_marks at americancentury.com > > > <toby_marks at americancentury.com> wrote: > > > > 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 observed examples in the archives using this type of approach. Does > > > > anybody have a snippet of a call to apply() that would accomplish this as > > > > well? > > > > > > > > Thanks! > > > > > > > > > > > > seed=50 > > > > $a = array(rnorm(20),dim=c(4,5)) > > > > $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) > > > > $a > > > > [,1] [,2] [,3] [,4] [,5] > > > > [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 > > > > [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 > > > > [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 > > > > [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 > > > > > > > > $a/b > > > > [,1] [,2] [,3] [,4] [,5] > > > > [1,] 1.0000000 1.0000000 1.00000000 1.0000000 1.000000 > > > > [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 > > > > [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 > > > > [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 > > > > > > > > > > > > > > > > ------------------------------------------------------------ > > > > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > > > > > > > ______________________________________________ > > > > R-help at stat.math.ethz.ch 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. > > > > > > > > > >
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 to be more efficient still (at least in respect of Prof. Grothendieck's toy example) by between 20 and 25 percent: > a <- matrix(1:24,4) > system.time(for(i in 1:1000) junk <- a / rep(a[1,], each = 4)) [1] 0.690 0.080 1.051 0.000 0.000 > system.time(for(i in 1:1000) junk <- t(t(a)/a[1,])) [1] 0.520 0.120 0.647 0.000 0.000 > system.time(for(i in 1:10000) junk <- a / rep(a[1,], each = 4)) [1] 7.08 0.99 10.08 0.00 0.00 > system.time(for(i in 1:10000) junk <- t(t(a)/a[1,])) [1] 5.530 0.940 7.856 0.000 0.000 cheers, Rolf Turner