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.
[GFGTABS]
R
# R program to create a matrix
m <- matrix(1:8, nrow=2)
print(m)
[/GFGTABS]
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.
[GFGTABS]
R
# R program for matrix multiplication
# with a scalar
m <- matrix(1:8, nrow=2)
m <- 2*m
print(m)
[/GFGTABS]
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.
[GFGTABS]
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)
[/GFGTABS]
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.
[GFGTABS]
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)
[/GFGTABS]
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 be of dimension M*Z.
[GFGTABS]
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)
[/GFGTABS]
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
Similar Reads
Matrix multiplication in R using for loop
The matrix multiplication is a basic operation in the linear algebra. In R Programming Language we can perform matrix multiplication using a for loop, which provides a basic understanding of the underlying process involved. In R language, we can perform matrix multiplication using a for loop, which
4 min read
Matrix Multiplication Practice Questions
A matrix is a set of numbers arranged in rows and columns to form a rectangular array. Multiplying a matrix by another matrix is called "matrix multiplication". In this article, we will learn what matrix multiplication is. And practice some questions related to it. What is Matrix Multiplication?In l
5 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
How to perform multiplication in R
Multiplication is a fundamental arithmetic operation that is essential in various fields, including data analysis, statistics, and machine learning. The R Programming Language provides robust support for performing multiplication, whether it's simple scalar multiplication, element-wise multiplicatio
3 min read
Matrix in R - Arithmetic Operations
Arithmetic operations include addition (+), subtraction (-), multiplication(*), division (/) and modulus(%). In this article we are going to see the matrix creation and arithmetic operations on the matrices in R programming language. ApproachCreate first matrix Syntax: matrix_name <- matrix(data
3 min read
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
Algebraic Operations on a Matrix in R
In this article, we will discuss how we perform Algebraic Operations on a Matrix in R Programming Language. so we will start with the matrix. What is Matrix?A Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and colum
8 min read
Array vs Matrix in R Programming
The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar
3 min read
Matrix Transpose in R
Transpose of a matrix is an operation in which we convert the rows of the matrix in column and column of the matrix in rows. The general equation for performing the transpose of a matrix is as follows. Aij = Aji  where i is not equal to j Example: Matrix M ---> [1, 8, 9 12, 6, 2 19, 42, 3] Transpose
2 min read
Inverse of Matrix in R
The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variables. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity mat
3 min read