# Matrix Multiplication in R

Last Updated : 12 Mar, 2024

Matrix multiplication is the most useful matrix operation. It is widely used in areas such as network theory, transformation of coordinates and many more uses nowadays. A matrix in R can be created using matrix() function and this function takes input vector, nrow, ncol, byrow, dimnames as arguments.

Creating a matrix
A matrix can be created using matrix() function.

R ``` # R program to create a matrix m <- matrix(1:8, nrow=2) print(m) ```

Output:

`     [,1] [,2] [,3] [,4][1,]    1    3    5    7[2,]    2    4    6    8`

### Multiplication of Matrices

The multiplication operator * is used for multiplying a matrix by scalar or element-wise multiplication of two matrices.
Multiplication with scalar
If you multiply a matrix with a scalar value, then every element of the matrix will be multiplied with that scalar.

R ``` # R program for matrix multiplication # with a scalar m <- matrix(1:8, nrow=2) m <- 2*m print(m) ```

Output:

`     [,1] [,2] [,3] [,4][1,]    2    6   10   14[2,]    4    8   12   16`

In the above code, the scalar is multiplied with every element of the original matrix. This is how the multiplication process takes place.

`2*1=2  2*3=6  2*5=10  2*7=142*2=4  2*4=8  2*6=12  2*8=16`

### Multiplication between Matrices

When a matrix is multiplied with another matrix, the element-wise multiplication of two matrices take place. All the corresponding elements of both matrices will be multiplied under the condition that both matrices will be of the same dimension.

R ``` # R program for matrix multiplication # Creating matrices m <- matrix(1:8, nrow=2) n <- matrix(8:15, nrow=2) # Multiplying matrices print(m*n) ```

Output:

`     [,1] [,2] [,3] [,4][1,]    8   30   60   98[2,]   18   44   78  120`

This is how the multiplication process takes place.

`1*8=8   3*10=30  5*12=60   7*14=982*9=18  4*11=44  6*13=78   8*15=120`

### Multiplication with Vector

If a matrix is multiplied with a vector then vector will be promoted to either row or column matrix to make two arguments conformable.

R ``` # R program for matrix multiplication # Creating matrix m <- matrix(1:8, nrow=2) # Creating a vector vec <- 1:2 # Multiplying matrix with vector print(vec*m) ```

Output:

`     [,1] [,2] [,3] [,4][1,]    1    3    5    7[2,]    4    8   12   16`

This is how the multiplication process takes place:

`             1*1=1   1*3=3   1*5=5   1*7=72*2=4   2*4=8   2*6=12  2*8=16`

### Multiplication using %*% operator

The Operator%*% is used for matrix multiplication satisfying the condition that the number of columns in the first matrix is equal to the number of rows in second. If matrix A[M, N] and matrix B[N, Z] are multiplied then the resultant matrix will be of dimension M*Z.

R ``` # R program for matrix multiplication # Creating matrices m <- matrix(1:8, nrow=2) n <- matrix(8:15, nrow=4) # Multiplying matrices using operator print(m %*% n) ```

Output:

`     [,1] [,2][1,]  162  226[2,]  200  280`

This is how multiplication takes place

`1*8+3*9+5*10+7*11 = 162      1*12+3*13+5*14+7*15=2262*8+4*9+6*10+8*11 = 200      2*12+4*13+6*14+8*15=280 `