Matrix Multiplication in R – %*% Operator

Matrices are a useful tool anytime you have data spread across related categories. This kind of data occurs frequency in statistics making it an important part of data science. There are several operations that you can perform on matrices in R and they include ways to multiply matrices together.

Matrixes in R

Matrices handling is an important part of data science and R is an excellent tool for handling them. One practical example would be a case where you have three models of cars that share three size motors of the same type. There are two matrices that you can easily produce for this. One would be the size gas tank each model has for each engine size. The other would be the gas mileage that each model car gets with each size engine. R makes setting up each matrix and then performing operations on them easy.

How to create a matrix.

Creating a matrix in R is quite simple, it involves the Matrix function that has the format of matrix(vector, ncol=columes, nrow=rows2) and it takes the vector and converts it to specified number of rows and columns. Now, the number of rows multiplied by the number of columns must equal the total number of elements in the vector. For example.

# matrix multiplication in R - setup 
> matrix(c(1,5,3,8), ncol=2, nrow=2)
      [,1] [,2]
 [1,]    1    3
 [2,]    5    8

Now, the columns, or rows can be omitted, and they will be calculated by R, however, the one given needs to be a multiple of the total number of elements.

Multiplication Operators

Multiplying matrices using a multiplication operator in R is one of a massive array of matrix operations and matrix algebra you can perform in R. R has two multiplication operators for matrices. The first is denoted by * which is the same as a simple multiplication sign. This operation does a simple element by element multiplication up to matrices.

# matrix multiplication in R - element by element 
> a = matrix(c(1,3,5,7), ncol=2, nrow=2)
 > a
      [,1] [,2]
 [1,]    1    5
 [2,]    3    7
 > b = matrix(c(2,4,6,8), ncol=2, nrow=2)
 > b
      [,1] [,2]
 [1,]    2    6
 [2,]    4    8
 > 
 > a*b
      [,1] [,2]
 [1,]    2   30
 [2,]   12   56

The second operator is denoted by %*% and it performs a matrix multiplication between the two matrices.

# matrix multiplication in R - algebraic
> a %*% b
      [,1] [,2]
 [1,]   22   46
 [2,]   34   74

> b %*% a
      [,1] [,2]
 [1,]   20   52
 [2,]   28   76

Note that the order of the matrices affects the results in matrix multiplication. The original matrix and the second matrix are each identified by a matrix multiplication operator, and are combined for a result of the product matrix. If you inverse the order of the original matrix and the second matrix, the result matrix will be slightly different than the matrix product of the first operation.

Applications

A good example of an element by element matrix multiplication equation is the one used above of three models of cars that share three size motors of the same type. Element wise multiplication takes each column vector and row vector and multiplies them together to get the matrix vector product. One would be the size gas tank each model has for each engine size in gallons. Each column refers to the model in each row refers to the engine.

# matrix multiplication in R - example
> gt = matrix(c(15,20,25,15,20,25,15,20,25), ncol=3, nrow=3)
 > gt
      [,1] [,2] [,3]
 [1,]   15   15   15
 [2,]   20   20   20
 [3,]   25   25   25

The other would be the mileage that each model car gets with each size engine. Likewise, each column refers to the model in each row refers to the engine.

# matrix multiplication in R - example
> m = matrix(c(35,26,18,30,25,17,37,28,20), ncol=3, nrow=3)
 > m
      [,1] [,2] [,3]
 [1,]   35   30   37
 [2,]   26   25   28
 [3,]   18   17   20

When you multiply these two matrices in an element by element manner you get the total number of miles that each vehicle will go on a single tank of gas.

# matrix multiplication in R - example
> gt*m
      [,1] [,2] [,3]
 [1,]  525  450  555
 [2,]  520  500  560
 [3,]  450  425  500

The applications of matrix and scalar multiplication are endless. One common application of multiplying matrices is in the transformation between coordinate systems where the matrix is the coordinates of unit vectors from one coordinate system in another. You transpose the coordinates from matrix A to matrix B, for a linear transformation from one coordinate system to another in a product matrix. These applications have uses in physics and data science which is why R is designed to make these calculations easy.

Matrix multiplication in R is amazingly easy. In most programming languages to do these calculations requires multiple lines of code to handle each part of the operation. In R matrix multiplication it is done with a single operation. While you have two different operations for two different types of multiplication then work together to keep the process as simple as possible.

Scroll to top
Privacy Policy