R is an important programming language used heavily by statisticians. It is also used in machine learning, data science, research, and many more new fields. It is a computing environment where statistical data may be implemented.
In a previous
article, we learned how to add two matrices in R. In this article, we shall learn the multiplication between two matrices in R. Apart from this, we shall also learn the major difference between two important operators in R, which are * and %*%.
Let's get started now.
Operators * And %*% In R
The * operator is a simple arithmetic operator. It is called the multiplication operator. The %*% operator is a special kind of multiplication operator. It is used in the multiplication of matrices in R. Now, we shall see how these two work differently, with examples. To see the differences between these two operators, we shall use both operators with two numbers separately.
- #Using Simple Multiplication Operator
- 2 * 2
Output
> 2 * 2
[1] 4
As shown above, we have used a simple multiplication operator (*). Now, we shall use a matrix multiplication operator %*% with the same values as above.
- #Using Matrix Multiplication Operator
- 2%*%2
Output
> 2%*%2
[,1]
[1,] 4
Now, we can see the differences clearly as with both operators, the operands are the same but the outputs are different. In a simple multiplication operator (*), the output has been generated as a vector while in the matrix multiplication operator, the output has been generated as a matrix of one row and one column. It has been shown by the below image in R studio on how it works.
Now, we shall see and learn in detail how matrix multiplication works in R using matrix multiplication operator which is %*%.
Creating Matrices For Multiplication
Let's first understand the problem statement in the following image where we have to find the output after multiplying MatrixA and MatrixB.
As we can see in the above image, both matrices are of the same dimensions and the same elements. So, to work with that, first, we shall create a matrix in R. It has been created using the below R script in R Studio.
-
- Vector1 <- c(5, 2)
-
- MatrixA <- matrix(data=Vector1, nrow = 1, ncol = 2)
-
- MatrixA <- rbind(MatrixA, c(4, 3))
-
-
- MatrixA
-
- MatrixB <- MatrixA
-
- MatrixB
Output
> MatrixA
[,1] [,2]
[1,] 5 2
[2,] 4 3
> MatrixB
[,1] [,2]
[1,] 5 2
[2,] 4 3
Here, first, we created a vector of the name Vector1. The matrix of name MatrixA has been created by the elements of previously created vector Vector1. The function matrix is used to create a matrix in R. Again, we added an extra row to that matrix using rbind() function as shown above.
Matrix B has been created by copying and assigning all the elements of MatrixA.
Matrices Multiplication With Operator %*%
Now, we shall multiply both matrices and see the output in R studio. To multiply matrices special type of matrix multiplication operator %*% is used. It has been shown below with the help of R script.
- #Multiplying Both Matrices By %*% Operator
- MatrixA %*% MatrixB
Output
> MatrixA %*% MatrixB
[,1] [,2]
[1,] 33 16
[2,] 32 17
It has been shown below in the image.
In R Studio, it looks like the below image.
The output and multiplication have been illustrated by the following example image.
Now we have already seen how to multiply two matrices in R with examples in R studio by R script as shown above. We can also use a simple multiplication operator but it will work as a normal multiplication. It will simply multiply the elements of the same index.
So, to multiply two matrices in R special type of operator i.e., matrix multiplication operator (%*%) is used in R.
Summary
In this article, we have seen how to perform arithmetic operations; i.e., multiplication on matrices in R. Apart from this we have seen the major difference between simple multiplication operator * and special type of matrix multiplication operator %*% in R. While working with the above multiplication operation between two matrices we were reminded to create a vector, matrix and using the rbind() function to add a new row in already created and available matrix.
I hope you learned and enjoyed it. I look forward to seeing your feedback.