Search code examples
rmultidimensional-arraymatrix-multiplication

How to do elements wise addition or multiplication R with 4 X 3 and 3 X 5 vector in R


I'm trying to do element wise multiplication of two matrix a and b I'm getting the error saying

Error in a + b : non-conformable arrays

data <- matrix(151:162, nrow=4)
data2 <- matrix(221:235, nrow=3)

Error in a * b : non-conformable arrays

However when I'm doing actual matrix multiplication I'm getting the desired output. Can anyone suggest me how to fix that.


Solution

  • 150:162 are 13 elements and 220:235 are 16. length(150:162) will show. These aren't 4x3 or 3x5 matrices.

    Update: For elementwise operation the dimensions of the matrices need to be identical. See size(data). So these operations are not possible with 3x4 and 4x3.