32

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)%*%c(1:2)
     [,1]
[1,]    5
lokheart
  • 23,743
  • 39
  • 98
  • 169

4 Answers4

26

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

Community
  • 1
  • 1
MadSeb
  • 7,958
  • 21
  • 80
  • 121
9
> 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
jackStinger
  • 2,035
  • 5
  • 23
  • 36
5

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/

From Matrix Multiplication Wikipedia Page

Roasty247
  • 679
  • 5
  • 20
0

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

Manjitha Teshara
  • 562
  • 2
  • 8
  • 21
  • 1
    As other answers already state, it is used to multiply any two matrices. Doesn't matter if one is a transpose, regardless of what your tutorial site says. – Gregor Thomas Apr 16 '19 at 13:38
  • `> M = matrix(c(3,2,5,3,4,5),3, 2) > M [,1] [,2] [1,] 3 3 [2,] 2 4 [3,] 5 5 > M %*% M Error in M %*% M : non-conformable arguments > M %*% t(M) [,1] [,2] [,3] [1,] 18 18 30 [2,] 18 20 30 [3,] 30 30 50` I think the transpose matters if the matrix is multiplied by itself, otherwise the %*% produces an error https://stackoverflow.com/questions/33237043/error-in-r-nonconformable-arguments-not-true – Roasty247 Apr 16 '19 at 13:52
  • Dimensions must conform for matrix multiplication---this means *the number of **columns** of the first matrix must equal the number of **rows** of the second matrix*. This is always the case for a transpose, but the transpose is not necessary. Try `m1 = matrix(1:4, ncol = 2)` and `m2 = matrix(1:100, nrow = 2)`. `m1 %*% m2` works just fine. – Gregor Thomas Apr 16 '19 at 15:53
  • I'd recommend you do a bit of reading. For example, [here's the wikipedia page on matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication). You don't have to read far---the 4th sentence gives the conformable arguments requirement. You can also read the *Definition* section, and see that the term *transpose* is not used in the definition. – Gregor Thomas Apr 16 '19 at 15:57