multiply each columns of a matrix by a vector

6.5k views Asked by At

I have

mat<-matrix(1:12,3)
vect<-c(2,2,2) 

How can I multiply each column by the vector in order to obtain:

2,  8, 14, 20
4, 10, 16, 22
6, 12, 18, 24
1

There are 1 answers

1
Pierre L On BEST ANSWER
vect*mat
     [,1] [,2] [,3] [,4]
[1,]    2    8   14   20
[2,]    4   10   16   22
[3,]    6   12   18   24

The vector vect is recycled by column. Better to experiment with different values to see the process.