The R %*% operator

后端 未结 4 1856
无人及你
无人及你 2020-12-06 04:35

What is this? I can\'t find help by using ?. (Sorry for being dumb)

> 1%*%1
     [,1]
[1,]    1
> 10%*%10
     [,1]
[1,]  100
> c(1:2)%         


        
相关标签:
4条回答
  • 2020-12-06 04:48

    It's a matrix multiplication operator!

    From the documentation:

    Description:

    Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

    Usage:

    x %*% y
    

    Arguments:

    x, y    numeric or complex matrices or vectors

    0 讨论(0)
  • 2020-12-06 05:00

    I created a question 'What is the calculation behind the %*% operator in R?' which was marked as a duplicate of this question. The %*% operator is used to multiply two matrices. I didn't realise 'matrix multiplication' was an established algebraic method so it was useful to learn the underlying calculation, not yet described explicitly in other answers here. Passing on useful links from comments in the duplicate question

    https://en.m.wikipedia.org/wiki/Matrix_multiplication#Definition

    http://matrixmultiplication.xyz/

    0 讨论(0)
  • 2020-12-06 05:04

    This operator is used to multiply a matrix with its transpose.

    M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
    
    t = M %*% t(M)
    
    print(t)
    

    from tutorialspoints

    0 讨论(0)
  • 2020-12-06 05:06
    > c(1,2,3) %*% c(4,5,6)
         [,1]
    [1,]   32
    > c(1,2,3) * c(4,5,6)
    [1]  4 10 18
    

    Like MadSeb said, it is the matrix multiplication operator. If you give it two vectors, it will coerce them to (logical) 1-row & a 1-col matrix and multiply them.

    It is also the inner (or dot) product between two vectors and finds wide usage in linear algebra, computational geometry and a host of other applications.

    http://en.wikipedia.org/wiki/Dot_product

    BTW, the vectors have to be in the same space (same number of dimensions)

    > c(1,2,3) %*% c(4,5,6,7)
    Error in c(1, 2, 3) %*% c(4, 5, 6, 7) : non-conformable arguments
    
    0 讨论(0)
提交回复
热议问题