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 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.
Multilication with scalar
If you multiply a matrix with a scalar value, then every element of the matrix will be multiplied with that scalar.
Example:
# 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=14 2*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.
Example:
# 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=98 2*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.
Example:
# 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=7 2*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 of dimension M*N.
Example:
# 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=226 2*8+4*9+6*10+8*11 = 200 2*12+4*13+6*14+8*15=280
Recommended Posts:
- Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
- Getting Multiplication of the Objects passed as Arguments in R Language - prod() Function
- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Matrix vs Dataframe in R
- Matrix Transpose in R
- Inverse of Matrix in R
- Array vs Matrix in R Programming
- Algebraic Operations on a Matrix in R
- Create Matrix from Vectors in R
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- How to read a Matrix from user in Java?
- Take Matrix input from user in Python
- Print matrix in zig-zag fashion from the last column
- Getting the Determinant of the Matrix in R Programming - det() Function
- Getting a Matrix of number of rows in R Programming - row() Function
- Calculate Trace of a Matrix in R Programming - tr() Function
- Finding Inverse of a Matrix in R Programming - inv() Function
- Getting a Matrix of number of columns in R Programming - col() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

