简体   繁体   中英

Best way to multiply a vector to columns of a matrix

I have a matrix mymat and a vector v . How can I multiply the vector v to each column of matrix mymat and get the following result?

    mymat = matrix(c(6, 4, 6, 2, 5, 8), nrow=3, ncol=2)
    mymat
    v  =c(2, 4, 6)
    > result
        [,1] [,2]
   [1,]   12    4
   [2,]   16   20
   [3,]   36   48
    > mymat * v
     [,1] [,2]
[1,]   12    4
[2,]   16   20
[3,]   36   48

simple as that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM