To perform matrix multiplication in R, you can use the %*%
operator. This operator is used to multiply two matrices according to the rules of linear algebra, where the number of columns in the first matrix must equal the number of rows in the second matrix.
In this example,
matrix1
with values from 1 to 4 arranged in 2 rows and 2 columns using the matrix()
function.matrix2
with values from 5 to 8 arranged in 2 rows and 2 columns using the matrix()
function.%*%
operator to multiply matrix1
by matrix2
. The multiplication is performed according to the rules of linear algebra, where each element of the resulting matrix is the sum of the products of corresponding elements from the rows of the first matrix and the columns of the second matrix.result_matrix
.result_matrix
to standard output to see the result of the matrix multiplication.matrix1 <- matrix(1:4, nrow = 2, ncol = 2)
matrix2 <- matrix(5:8, nrow = 2, ncol = 2)
result_matrix <- matrix1 %*% matrix2
print(result_matrix)
[,1] [,2] [1,] 19 22 [2,] 43 50
In this example,
matrix1
with values from 1 to 6 arranged in 2 rows and 3 columns using the matrix()
function.matrix2
with values from 7 to 12 arranged in 3 rows and 2 columns using the matrix()
function.%*%
operator to multiply matrix1
by matrix2
. The multiplication is performed according to the rules of linear algebra, where each element of the resulting matrix is the sum of the products of corresponding elements from the rows of the first matrix and the columns of the second matrix.result_matrix
.result_matrix
to standard output to see the result of the matrix multiplication.matrix1 <- matrix(1:6, nrow = 2, ncol = 3)
matrix2 <- matrix(7:12, nrow = 3, ncol = 2)
result_matrix <- matrix1 %*% matrix2
print(result_matrix)
[,1] [,2] [1,] 58 64 [2,] 139 154
In this example,
matrix1
with values from 1 to 9 arranged in 3 rows and 3 columns using the matrix()
function.matrix2
with values from 10 to 18 arranged in 3 rows and 3 columns using the matrix()
function.%*%
operator to multiply matrix1
by matrix2
. The multiplication is performed according to the rules of linear algebra, where each element of the resulting matrix is the sum of the products of corresponding elements from the rows of the first matrix and the columns of the second matrix.result_matrix
.result_matrix
to standard output to see the result of the matrix multiplication.matrix1 <- matrix(1:9, nrow = 3, ncol = 3)
matrix2 <- matrix(10:18, nrow = 3, ncol = 3)
result_matrix <- matrix1 %*% matrix2
print(result_matrix)
[,1] [,2] [,3] [1,] 84 90 96 [2,] 201 216 231 [3,] 318 342 366
In this tutorial, we learned How to do Matrix Multiplication in R language with well detailed examples.