[R] Multiply row of a matrix

ac.dasilva at ulg.ac.be ac.dasilva at ulg.ac.be
Tue Feb 21 17:23:30 CET 2017


If we have the following matrix: 

Mat<-matrix(1:9, byrow=TRUE, nrow=3)
Mat

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

 I would like to have each row multiplied by a different number. 
So I would like row 1 multiplied by 3, row2 by 1 and row3 by 0.5
Which would give: 

     [,1] [,2] [,3]
[1,]    3    6    9
[2,]    4    5    6
[3,]    3.5  4    4.5


Whatever I try, I always obtain 
     [,1] [,2] [,3]
[1,]    3    2  1.5
[2,]   12    5  3.0
[3,]   21    8  4.5

Which corresponds to the columns multiplied by the factors 

Could anyone help we to get the proper result ?
Best, Anne-Christine da Silva



More information about the R-help mailing list