Multiplying two matrices in R

后端 未结 3 1239
花落未央
花落未央 2020-12-10 08:34

I have 2 matrices.

The first one: [1,2,3]

and the second one:

[3,1,2
 2,1,3
 3,2,1]

I\'m looking for a way to

3条回答
  •  执念已碎
    2020-12-10 09:30

    It's difficult to say what the best answer here is because the notation in the question isn't in R, it's in matlab. It's hard to tell if the questioner wants to multiple a vector, 1 row matrix, or 1 column matrix given the mixed notation.

    An alternate answer to this question is simply switch the order of the multiplication.

    v1 <- c(1,2,3)
    v2 <- matrix(c(3,1,2,2,1,3,3,2,1), ncol = 3, byrow = TRUE)
    v2 %*% v1
    

    This yields an answer that's a single column rather than a single row matrix.

提交回复
热议问题