Hi, I have this: y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) z = matrix(c(12, -6),ncol=2) In matlab I would do this> y .* xI would get this in matlab> ans0 -0 6 -3 12 -6 What is the equivalent in R? Thanks [[alternative HTML version deleted]]
> y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) > z = matrix(c(12, -6),ncol=2) > t(apply(y, 1, function(x) x*z))[,1] [,2] [1,] 0 0 [2,] 6 -3 [3,] 12 -6 I hope this helps. Chel Hee Lee On 14-11-19 08:22 AM, Ruima E. wrote:> Hi, > > I have this: > > y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) > z = matrix(c(12, -6),ncol=2) > > In matlab I would do this > >> y .* x > I would get this in matlab > >> ans > 0 -0 > 6 -3 > 12 -6 > > What is the equivalent in R? > > Thanks > > [[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.
Thank you Chel Hee. Isn't there a simpler way to do so? On Wed, Nov 19, 2014 at 3:35 PM, Chel Hee Lee <chl948 at mail.usask.ca> wrote:>> y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) >> z = matrix(c(12, -6),ncol=2) >> t(apply(y, 1, function(x) x*z)) > [,1] [,2] > [1,] 0 0 > [2,] 6 -3 > [3,] 12 -6 > > I hope this helps. > > Chel Hee Lee > > On 14-11-19 08:22 AM, Ruima E. wrote: >> Hi, >> >> I have this: >> >> y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) >> z = matrix(c(12, -6),ncol=2) >> >> In matlab I would do this >> >>> y .* x >> I would get this in matlab >> >>> ans >> 0 -0 >> 6 -3 >> 12 -6 >> >> What is the equivalent in R? >> >> Thanks >> >> [[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. >
On 19-11-2014, at 15:22, Ruima E. <ruimaximo at gmail.com> wrote:> Hi, > > I have this: > > y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) > z = matrix(c(12, -6),ncol=2) > > In matlab I would do this > >> y .* x > > I would get this in matlab > >> ans > 0 -0 > 6 -3 > 12 -6 > > What is the equivalent in R? >One way of doing this could be: y * rep(z,1,each=nrow(y)) Berend> Thanks > > [[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.
Hi, just for the records, your original code seems incorrect, see inline. On 11/19/2014 03:22 PM, Ruima E. wrote:> Hi, > > I have this: > > y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) > z = matrix(c(12, -6),ncol=2) > > In matlab I would do this > >> y .* xHere you wrote 'x' which I guess refers to 'z', and should be repmat(z, size(y, 1), 1) in matlab (assuming 'z' is a row vector)> I would get this in matlab > >> ans > 0 -0 > 6 -3 > 12 -6 > > What is the equivalent in R? > > Thanks > > [[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.