is there a way to do element-by-element multiplication as in Gauss and MATLAB, as shown below? Thanks. --- a 1.0000000 2.0000000 3.0000000 x 1.0000000 2.0000000 3.0000000 2.0000000 4.0000000 6.0000000 3.0000000 6.0000000 9.0000000 a.*x 1.0000000 2.0000000 3.0000000 4.0000000 8.0000000 12.000000 9.0000000 18.000000 27.000000 -- Steven T. Yen, Professor of Agricultural Economics The University of Tennessee http://web.utk.edu/~syen/ [[alternative HTML version deleted]]
Did you even try? a <- 1:3 x <- matrix(c(1,2,3,2,4,6,3,6,9),3) a*x [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 8 12 [3,] 9 18 27 Michael On Fri, Nov 4, 2011 at 7:26 PM, Steven Yen <syen at utk.edu> wrote:> is there a way to do element-by-element multiplication as in Gauss > and MATLAB, as shown below? Thanks. > > --- > a > > ? ? ? ?1.0000000 > ? ? ? ?2.0000000 > ? ? ? ?3.0000000 > x > > ? ? ? ?1.0000000 ? ? ? ?2.0000000 ? ? ? ?3.0000000 > ? ? ? ?2.0000000 ? ? ? ?4.0000000 ? ? ? ?6.0000000 > ? ? ? ?3.0000000 ? ? ? ?6.0000000 ? ? ? ?9.0000000 > a.*x > > ? ? ? ?1.0000000 ? ? ? ?2.0000000 ? ? ? ?3.0000000 > ? ? ? ?4.0000000 ? ? ? ?8.0000000 ? ? ? ?12.000000 > ? ? ? ?9.0000000 ? ? ? ?18.000000 ? ? ? ?27.000000 > > > -- > Steven T. Yen, Professor of Agricultural Economics > The University of Tennessee > http://web.utk.edu/~syen/ > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
There are a few (nasty?) side-effects to c(), one of which is stripping a matrix of its dimensionality. E.g., x <- matrix(1:4, 2) c(x) [1] 1 2 3 4 So that's probably what happened to you. R has a somewhat odd feature of not really considering a pure vector as a column or row vector but being willing to change it to either: e.g. y <- 1:2 x %*% y y %*% x y %*% y while matrix(y) %*% x throws an error, which can also trip folks up. You might also note that x * y and y*x return the same thing in this problem. Getting back to your problem: what are v and b and what are you hoping to get done? Specifically, what happened when you tried v*b (give the exact error message). It seems likely that they are non-conformable matrices, but here non-conformable for element-wise multiplication doesn't mean the same thing as it does for matrix multiplication. E.g., x <- matrix(1:4,2) y <- matrix(1:6,2) dim(x) [1] 2 2 dim(y) [1] 2 3 x * y -- here R seems to want matrices with identical dimensions, but i can't promise that. x %*% y does work. Hope this helps and yes I know it can seem crazy at first, but there really is reason behind it at the end of the tunnel, Michael On Sun, Nov 6, 2011 at 12:11 AM, Steven Yen <syen at utk.edu> wrote:> My earlier attempt > > ?? dp <- v*b > > did not work. Then, > > ?? dp <- c(v)*b > > worked. > > Confused, > > Steven > > At 09:10 PM 11/4/2011, you wrote: > > Did you even try? > > a <- 1:3 > x <-? matrix(c(1,2,3,2,4,6,3,6,9),3) > a*x > > ???? [,1] [,2] [,3] > [1,]??? 1??? 2??? 3 > [2,]??? 4??? 8?? 12 > [3,]??? 9?? 18?? 27 > > Michael > > On Fri, Nov 4, 2011 at 7:26 PM, Steven Yen <syen at utk.edu> wrote: >> is there a way to do element-by-element multiplication as in Gauss >> and MATLAB, as shown below? Thanks. >> >> --- >> a >> >>??????? 1.0000000 >>??????? 2.0000000 >>??????? 3.0000000 >> x >> >>??????? 1.0000000??????? 2.0000000??????? 3.0000000 >>??????? 2.0000000??????? 4.0000000??????? 6.0000000 >>??????? 3.0000000??????? 6.0000000??????? 9.0000000 >> a.*x >> >>??????? 1.0000000??????? 2.0000000??????? 3.0000000 >>??????? 4.0000000??????? 8.0000000??????? 12.000000 >>??????? 9.0000000??????? 18.000000??????? 27.000000 >> >> >> -- >> Steven T. Yen, Professor of Agricultural Economics >> The University of Tennessee >> http://web.utk.edu/~syen/ >>??????? [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > -- > Steven T. Yen, Professor of Agricultural Economics > The University of Tennessee > http://web.utk.edu/~syen/
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Nov-07 00:02 UTC
[R] Matrix element-by-element multiplication
It looks like pdf is not a "scalar" (that term actually has no meaning in R but I know what you mean) but is rather a 1x1 matrix, as attested by the fact it has dimensions. If you give dnorm() a matrix it will return one, as it did here. Perhaps you should look at the is.matrix() and as.vector() functions rather than abusing a side-effect of c(), which makes it much more difficult to see R's internal logic, which, while quirky, is useful at the end of the day. Michael PS - It's good form to cc the list at each step so others can follow along and contribute when I say something wrong. It also helps you get quicker answers. On Nov 6, 2011, at 1:06 AM, Steven Yen <syen@utk.edu> wrote:> I am trying to multiply what I know is a scalar (pdf(xb)) to a column vector of coefficient (bb). > In the following, pdf is a scalar and bb is 5 x 1. I first show what worked and then what did not work. > If my pdf is a scalar, why would I need c(pdf) to be able to pre-multiply it by a 5 x 1 vector? > > --- > > > x <- as.matrix(colMeans(x)) > > xb <- t(x)%*%bb > > pdf <- dnorm(xb) > > > dim(bb) > [1] 5 1 > > > > > cpdf <- c(pdf) > > dim(cpdf) > NULL > > cpdf > [1] 0.304201 > > (dphat <- cpdf*bb) > [,1] > (Intercept) 0.32744753 > xrage -0.00599225 > xryr 0.01758431 > xrrate -0.08217250 > xrrel -0.05695434 > > > > pdf <- dnorm(xb) > > dim(pdf) > [1] 1 1 > > pdf > [,1] > [1,] 0.304201 > > (dphat <- pdf*bb) > Error in pdf * bb : non-conformable arrays > > > > At 12:21 AM 11/6/2011, you wrote: >> There are a few (nasty?) side-effects to c(), one of which is >> stripping a matrix of its dimensionality. E.g., >> >> x <- matrix(1:4, 2) >> c(x) >> [1] 1 2 3 4 >> >> So that's probably what happened to you. R has a somewhat odd feature >> of not really considering a pure vector as a column or row vector but >> being willing to change it to either: >> >> e.g. >> >> y <- 1:2 >> >> x %*% y >> y %*% x >> y %*% y >> >> while matrix(y) %*% x throws an error, which can also trip folks up. >> You might also note that x * y and y*x return the same thing in this >> problem. >> >> Getting back to your problem: what are v and b and what are you hoping >> to get done? Specifically, what happened when you tried v*b (give the >> exact error message). It seems likely that they are non-conformable >> matrices, but here non-conformable for element-wise multiplication >> doesn't mean the same thing as it does for matrix multiplication. >> E.g., >> >> x <- matrix(1:4,2) >> y <- matrix(1:6,2) >> >> dim(x) >> [1] 2 2 >> >> dim(y) >> [1] 2 3 >> >> x * y -- here R seems to want matrices with identical dimensions, but >> i can't promise that. >> >> x %*% y does work. >> >> Hope this helps and yes I know it can seem crazy at first, but there >> really is reason behind it at the end of the tunnel, >> >> Michael >> >> >> On Sun, Nov 6, 2011 at 12:11 AM, Steven Yen <syen@utk.edu> wrote: >> > My earlier attempt >> > >> > dp <- v*b >> > >> > did not work. Then, >> > >> > dp <- c(v)*b >> > >> > worked. >> > >> > Confused, >> > >> > Steven >> > >> > At 09:10 PM 11/4/2011, you wrote: >> > >> > Did you even try? >> > >> > a <- 1:3 >> > x <- matrix(c(1,2,3,2,4,6,3,6,9),3) >> > a*x >> > >> > [,1] [,2] [,3] >> > [1,] 1 2 3 >> > [2,] 4 8 12 >> > [3,] 9 18 27 >> > >> > Michael >> > >> > On Fri, Nov 4, 2011 at 7:26 PM, Steven Yen <syen@utk.edu> wrote: >> >> is there a way to do element-by-element multiplication as in Gauss >> >> and MATLAB, as shown below? Thanks. >> >> >> >> --- >> >> a >> >> >> >> 1.0000000 >> >> 2.0000000 >> >> 3.0000000 >> >> x >> >> >> >> 1.0000000 2.0000000 3.0000000 >> >> 2.0000000 4.0000000 6.0000000 >> >> 3.0000000 6.0000000 9.0000000 >> >> a.*x >> >> >> >> 1.0000000 2.0000000 3.0000000 >> >> 4.0000000 8.0000000 12.000000 >> >> 9.0000000 18.000000 27.000000 >> >> >> >> >> >> -- >> >> Steven T. Yen, Professor of Agricultural Economics >> >> The University of Tennessee >> >> http://web.utk.edu/~syen/ >> >> [[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. >> >> >> > >> > -- >> > Steven T. Yen, Professor of Agricultural Economics >> > The University of Tennessee >> > http://web.utk.edu/~syen/ > -- > Steven T. Yen, Professor of Agricultural Economics > The University of Tennessee > http://web.utk.edu/~syen/[[alternative HTML version deleted]]
Hello Steven, It looks like, there is no in-built function that can do GAUSS ".*" element-wise multiplication. Now, if you want to make the desired computations in R, it is actually preatty straightforward.> a<-c(1,2,3) > b<-matrix(rep(1:9,1),3,3,byrow=TRUE) > a*bThat, should work fine. But, suppose that for some reason you have following situation, which can make you trip for hours of sleepless nights. That is, you have a matrix "b", where number of columns equal to number of columns of your vector "a". That is> b<-matrix(rep(1:9,1),3,3,byrow=TRUE);b[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9> a <- matrix(rep(1:3,1),1,3,byrow=TRUE)[,1] [,2] [,3] [1,] 1 2 3 If you try to do elementwise multilication, i.e., of those two> b*aYou get an error that they are not comfomable, that is why, you have to write your own function (here, I just write the for-loop):> for ( i in 1:3 ) {foo[ ,i] = ( foo[ ,i] * bar[1,i] ) ; } [,1] [,2] [,3] [1,] 1 4 9 [2,] 4 10 18 [3,] 7 16 27 I hope that this helped Serge Boris Nesterenko CEO 50 Pence Music Production https://www.linkedin.com/in/sergenesterenko -- View this message in context: http://r.789695.n4.nabble.com/Matrix-element-by-element-multiplication-tp3992206p4712964.html Sent from the R help mailing list archive at Nabble.com.
On 30/09/15 12:49, waddawanna wrote:> Hello Steven, > > It looks like, there is no in-built function that can do GAUSS ".*" > element-wise multiplication. > Now, if you want to make the desired computations in R, it is actually > preatty straightforward. > >> a<-c(1,2,3) >> b<-matrix(rep(1:9,1),3,3,byrow=TRUE) >> a*b > > That, should work fine. But, suppose that for some reason you have following > situation, which can make you trip for hours of sleepless nights. That is, > you have a matrix "b", where number of columns equal to number of columns of > your vector "a". That is > >> b<-matrix(rep(1:9,1),3,3,byrow=TRUE);b > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 4 5 6 > [3,] 7 8 9 > >> a <- matrix(rep(1:3,1),1,3,byrow=TRUE) > [,1] [,2] [,3] > [1,] 1 2 3 > > If you try to do elementwise multilication, i.e., of those two >> b*a > > You get an error that they are not comfomable, that is why, you have to > write your own function (here, > I just write the for-loop): >> for ( i in 1:3 ) { > foo[ ,i] = ( foo[ ,i] * bar[1,i] ) ; > } > [,1] [,2] [,3] > [1,] 1 4 9 > [2,] 4 10 18 > [3,] 7 16 27 > > I hope that this helped.(1) You're making heavy weather by using rep() totally unnecessarily. (2) The example you give can be done much more succinctly; for-loops are unnecessary: t(as.vector(a)*t(b)) (3) Please don't post to R-help via nabble (WTF ever that is). It messes up everything. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
On Sep 29, 2015, at 4:49 PM, waddawanna wrote:> Hello Steven, > > It looks like, there is no in-built function that can do GAUSS ".*" > element-wise multiplication. > Now, if you want to make the desired computations in R, it is actually > preatty straightforward. > >> a<-c(1,2,3) >> b<-matrix(rep(1:9,1),3,3,byrow=TRUE) >> a*b > > That, should work fine. But, suppose that for some reason you have following > situation, which can make you trip for hours of sleepless nights. That is, > you have a matrix "b", where number of columns equal to number of columns of > your vector "a". That is > >> b<-matrix(rep(1:9,1),3,3,byrow=TRUE);b > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 4 5 6 > [3,] 7 8 9 > >> a <- matrix(rep(1:3,1),1,3,byrow=TRUE) > [,1] [,2] [,3] > [1,] 1 2 3 > > If you try to do elementwise multilication, i.e., of those two >> b*a > > You get an error that they are not comfomable,You should not have gotten that error with either `*` or `%*%`.> that is why, you have to > write your own function (here, > I just write the for-loop): >> for ( i in 1:3 ) { > foo[ ,i] = ( foo[ ,i] * bar[1,i] ) ; > } > [,1] [,2] [,3] > [1,] 1 4 9 > [2,] 4 10 18 > [3,] 7 16 27You were expecting c(1,2,3) to be a row-vector. That leads to disappointment. If anything it will be a column-vector. Notice .... no error:> a*b[,1] [,2] [,3] [1,] 1 2 3 [2,] 8 10 12 [3,] 21 24 27 You really wanted to sweep that vector> sweep(b, 1,a,'*') # Sweeping by rows is same a multiplying by recycle column vector[,1] [,2] [,3] [1,] 1 2 3 [2,] 8 10 12 [3,] 21 24 27> sweep(b, 2, a, '*') # You wanted to "sweep" across columns[,1] [,2] [,3] [1,] 1 4 9 [2,] 4 10 18 [3,] 7 16 27 -- David Winsemius Alameda, CA, USA