r - Elementwise Product of Matrix and Vector -


instead of usual matrix multiplication, need elementwise operation. following works fine:

# works bmat <- structure(c(3l, 3l, 10l, 3l, 4l, 10l, 5l, 8l, 8l, 8l, 3l, 8l, 8l, 2l, 6l, 10l, 2l, 8l, 3l, 9l), .dim = c(10l, 2l)) yvec <- c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) bmat * yvec #       [,1] [,2] #  [1,]    6    6 #  [2,]    6   16 #  [3,]   20   16 #  [4,]    6    4 #  [5,]    8   12 #  [6,]   20   20 #  [7,]   10    4 #  [8,]   16   16 #  [9,]   16    6 # [10,]   16   18 

however, following fails:

# fails amat <- structure(c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), .dim = c(10l, 2l)) xvec <- structure(c(1.83475682418716, 1.48154883122634, 1, 1, 1, 1, 1, 1, 1, 1), .dim = c(10l, 1l)) amat * xvec #fehler in amat * xvec : nicht passende arrays 

why that? has bmat being integer matrix? how can make second code work?

class(xvec) [1] "matrix"  dim(xvec) [1] 10  1  class(amat) [1] "matrix"      dim(amat) [1] 10  2 

element wise multiplication between 2 matrices possible when have same dimension. so, solution convert xvec vector. try

amat * c(xvec) #or amat * as.vector(xvec) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -