[R] Equivalent to matlab ".*" operator in R

Boris Steipe boris.steipe at utoronto.ca
Wed Nov 19 17:48:42 CET 2014


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



More information about the R-help mailing list