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
13 messages · Ruima E., Chel Hee Lee, Berend Hasselman +7 more
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
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
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?
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.
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.
Another (simpler) way that I can think is that
y * matrix(rep(z,3), ncol=ncol(y), byrow=TRUE)
[,1] [,2] [1,] 0 0 [2,] 6 -3 [3,] 12 -6 I hope this helps. Chel Hee Lee
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.
______________________________________________ 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))
another is t(t(y) * as.vector(z)) --Ista
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.
______________________________________________ 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, It is better to use sweep() for these kinds of problems, see ?sweep y <- matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) z <- matrix(c(12, -6),ncol=2) sweep(y, 2, z, "*") Best, Denes
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.
______________________________________________ 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.
When your matrices are the same size, the "*" operator does what you want. The problem is that you have to make a conforming version of z before you can use that operator.
y*matrix(rep(z,3),ncol=2,byrow=TRUE)
or
y*matrix(rep(z,each=3),ncol=2)
To interpret this, just keep in mind that matrices are folded vectors in R... every matrix can be thought of as a linear vector of columnwise data with dimension attributes.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
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. ______________________________________________ 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.
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
Here 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.
It can be simplified a bit, though, as the second operand in the multiplication does not need to be a matrix: y * rep(z,each=3)
When your matrices are the same size, the "*" operator does what you want. The problem is that you have to make a conforming version of z before you can use that operator.
y*matrix(rep(z,3),ncol=2,byrow=TRUE)
or
y*matrix(rep(z,each=3),ncol=2)
To interpret this, just keep in mind that matrices are folded vectors in R... every matrix can be thought of as a linear vector of columnwise data with dimension attributes.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On November 19, 2014 6:43:55 AM PST, "Ruima E." <ruimaximo at gmail.com> wrote:
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. ______________________________________________ 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.
______________________________________________ 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.
Or ... if you mean "simpler" as in "less to type", you can define your own binary operator by enclosing it in "%" signs, and the assign any of the previously proposed solutions, e.g.
y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2)
z = matrix(c(12, -6),ncol=2)
'%.*%' <- function(a,b) {a * rep(b, each=3)}
y %.*% z
[,1] [,2]
[1,] 0 0
[2,] 6 -3
[3,] 12 -6
What you have written does not work in Matlab -
y = [0 0;0.5 0.5;1 1]
y =
0 0
0.5000 0.5000
1.0000 1.0000
z = [12, -6]
z =
12 -6
.
y .* z
Error using .* Matrix dimensions must agree. When dimensions agree it you get the same result in R as in Matlab by using the * operator
y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) y
[,1] [,2] [1,] 0.0 0.0 [2,] 0.5 0.5 [3,] 1.0 1.0
z = matrix(1:6, ncol=2) z
[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6
prod <- matrix(y*z,2) prod
[,1] [,2] [,3] [1,] 0 3 2.5 [2,] 1 0 6.0
prod <- matrix(y*z) prod
[,1] [,2] [1,] 0 0.0 [2,] 1 2.5 [3,] 3 6.0
John C Frain 3 Aranleigh Park Rathfarnham Dublin 14 Ireland www.tcd.ie/Economics/staff/frainj/home.html mailto:frainj at tcd.ie mailto:frainj at gmail.com
Or ... if you mean "simpler" as in "less to type", you can define your own
binary operator by enclosing it in "%" signs, and the assign any of the
previously proposed solutions, e.g.
y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2)
z = matrix(c(12, -6),ncol=2)
'%.*%' <- function(a,b) {a * rep(b, each=3)}
y %.*% z
[,1] [,2]
[1,] 0 0
[2,] 6 -3
[3,] 12 -6
______________________________________________ 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.
Not sure you really want the overhead of sweep() for this, but logically, you want z as a vector or maybe 1d array since sweep() is designed as complimentary to apply(). I.e., you can sweep out marginal means using, say, m <- matrix(c(5, 7, 9, 13), 2) colmean <- apply(m, 2, mean) sweep(m, 2, colmean, "-") in which colmean is a vector. In general, apply() yields an array with fewer extents, and sweep expects one. -pd
Hi, It is better to use sweep() for these kinds of problems, see ?sweep y <- matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2) z <- matrix(c(12, -6),ncol=2) sweep(y, 2, z, "*") Best, Denes On 11/19/2014 03:50 PM, Berend Hasselman wrote:
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.
______________________________________________ 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.
______________________________________________ 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.
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com