Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Bert Gunter
OK, just for fun, I added a couple of *obvious* (or should have been) approaches and got results that exactly agreed with Peter D's comments: all O(n^2) results were essentially the same, and my matrix multiplication O(n^3) solution was **far** worse. So I'm not sure quite where Jeff's result came

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Stefan Evert
> On 4 Nov 2016, at 17:35, peter dalgaard wrote: > > Notice though, that Bert loses (or _should_ lose) for larger values of N, > since that method involves O(N^3) operations whereas the other two are > O(N^2). I am a bit surprised that sweep() is so inefficient even at N=1000. I also got diff

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Jeff Newmiller
If you want to get a comparable result, then run the same code. You may not want a comparable result though... your version straightens out the issue Peter was puzzled by. -- Sent from my phone. Please excuse my brevity. On November 4, 2016 11:35:02 AM PDT, Berend Hasselman wrote: > >> On 4 N

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Dimitri Liakhovitski
I love R for it: if you experiment enough with t() you can find a way to multiply almost anything by almost anything! :) On Thu, Nov 3, 2016 at 5:32 PM, Rui Barradas wrote: > Hello, > > Take advantage that in R '*' recycles its arguments (the shorter one) and > that the operation is performed col

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Bert Gunter
Berend, et. al.: "I don't quite understand why I get a very different ranking." Nor do I. But, as Peter noted, my matrix multiplication solution "should" be worst in terms of efficiency, and it clearly is. The other two *should* be "similar", and they are. So your results are consistent with what

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Berend Hasselman
> On 4 Nov 2016, at 19:27, Berend Hasselman wrote: > >> >> On 4 Nov 2016, at 16:41, Jeff Newmiller wrote: >> >> Sara wins on memory use. >> >> Rui wins on speed. >> >> Bert wins on clarity. >> >> library(microbenchmark) >> >> N <- 1000 >> x <- matrix( runif( N*N ), ncol=N ) >> y <- seq.in

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Berend Hasselman
> On 4 Nov 2016, at 16:41, Jeff Newmiller wrote: > > Sara wins on memory use. > > Rui wins on speed. > > Bert wins on clarity. > > library(microbenchmark) > > N <- 1000 > x <- matrix( runif( N*N ), ncol=N ) > y <- seq.int( N ) > > microbenchmark( { t( y * t(x) ) } > , { x %*% d

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread peter dalgaard
Notice though, that Bert loses (or _should_ lose) for larger values of N, since that method involves O(N^3) operations whereas the other two are O(N^2). I am a bit surprised that sweep() is so inefficient even at N=1000. -pd On 04 Nov 2016, at 16:41 , Jeff Newmiller wrote: > Sara wins on mem

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Jeff Newmiller
Sara wins on memory use. Rui wins on speed. Bert wins on clarity. library(microbenchmark) N <- 1000 x <- matrix( runif( N*N ), ncol=N ) y <- seq.int( N ) microbenchmark( { t( y * t(x) ) } , { x %*% diag( y ) } , { sweep( x, 2, y, `*` ) } ) Unit: milli

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Dimitri Liakhovitski
Nice! Thanks a lot, everybody! Dimitri On Fri, Nov 4, 2016 at 10:35 AM, Bert Gunter wrote: > My goodness! > >> x %*% diag(y) > > [,1] [,2] > [1,]2 12 > [2,]4 15 > [3,]6 18 > > will do. > > -- Bert > > > > Bert Gunter > > "The trouble with having an open mind is that people

Re: [R] multiplying a matrix by a vector

2016-11-04 Thread Bert Gunter
My goodness! > x %*% diag(y) [,1] [,2] [1,]2 12 [2,]4 15 [3,]6 18 will do. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On

Re: [R] multiplying a matrix by a vector

2016-11-03 Thread Sarah Goslee
Like this? > sweep(x, 2, y, "*") [,1] [,2] [1,]2 12 [2,]4 15 [3,]6 18 > On Thu, Nov 3, 2016 at 5:05 PM, Dimitri Liakhovitski wrote: > Hello! > > I have a matrix x and a vector y: > > x <- matrix(1:6, ncol = 2) > y <- c(2,3) > > I need to multiply the first column of x by

Re: [R] multiplying a matrix by a vector

2016-11-03 Thread Rui Barradas
Hello, Take advantage that in R '*' recycles its arguments (the shorter one) and that the operation is performed column-wise: t(y * t(x)) Hope this helps, Rui Barradas Em 03-11-2016 21:05, Dimitri Liakhovitski escreveu: Hello! I have a matrix x and a vector y: x <- matrix(1:6, ncol = 2)

[R] multiplying a matrix by a vector

2016-11-03 Thread Dimitri Liakhovitski
Hello! I have a matrix x and a vector y: x <- matrix(1:6, ncol = 2) y <- c(2,3) I need to multiply the first column of x by 2 (y[1]) and the second column of x by 3 (y[2]). Of course, I could do this - but it's column by column: x[,1] <- x[,1] * y[1] x[,2] <- x[,2] * y[2] x Or I could repeat