Unexpected result of matrix multiplication in R -
r program not return expected matrix multiplication
a<- c(0,1,1,0) a<- matrix(a,2,2) b<- matrix(c(1,2,3,4),2,2,byrow=true) a*b
gives final answer matrix(c(0,2,3,0), ncol = 2, byrow=true)
:
[,1] [,2] [1,] 0 2 [2,] 3 0
but actual answer should matrix(c(3,4,1,2), ncol = 2, byrow=true)
[,1] [,2] [1,] 3 4 [2,] 1 2
you can use either %*%
or crossprod
matrix multiplication
> %*% b [,1] [,2] [1,] 3 4 [2,] 1 2 > crossprod(a, b) [,1] [,2] [1,] 3 4 [2,] 1 2
note result 2x2 matrix, if want vector in example, use matrix(crossprod(a, b), ncol=1)
Comments
Post a Comment