Operations on Matrices in R
Last Updated :
07 Aug, 2024
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 9 elements is shown below. \begin{bmatrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{bmatrix} This Matrix [M] has 3 rows and 3 columns. Each element of matrix [M] can be referred to by its row and column number. For example, a23 = 6 Order of a Matrix : The order of a matrix is defined in terms of its number of rows and columns. Order of a matrix = No. of rows × No. of columns Therefore Matrix [M] is a matrix of order 3 × 3.
Operations on Matrices
There are four basic operations i.e. DMAS (Division, Multiplication, Addition, Subtraction) that can be done with matrices. Both the matrices involved in the operation should have the same number of rows and columns.
Matrices Addition
The addition of two same ordered matrices M_r_*_c and N_r_*_c yields a matrix R_r_*_c where every element is the sum of corresponding elements of the input matrices.
Python
# R program to add two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
sum = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
In the above code, nrow(B) gives the number of rows in B and ncol(B) gives the number of columns. Here, sum is an empty matrix of the same size as B and C. The elements of sum are the addition of the corresponding elements of B and C through nested for loops. Using '+' operator for matrix addition: Similarly, the following R script uses the in-built operator +:
Python
# R program for matrix addition
# using '+' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B + C)
Output:
[,1] [,2] [,3]
[1,] 3+0i 5.5+0i 8+0i
[2,] 2+3i 6.0+0i 10+0i
R provides the basic inbuilt operator to add the matrices. In the above code, all the elements in the resultant matrix are returned as complex numbers, even if a single element of a matrix is a complex number. Properties of Matrix Addition:
- Commutative: B + C = C + B
- Associative: For n number of matrices A + (B + C) = (A + B) + C
- Order of the matrices involved must be same.
Matrices Subtraction
The subtraction of two same ordered matrices M_r_*_c and N_r_*_c yields a matrix R_r_*_c where every element is the difference of corresponding elements of the second input matrix from the first.
Python
# R program to add two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
diff = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating diff of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
diff[row, col] <- B[row, col] - C[row, col]
}
}
# Printing resultant matrix
print(diff)
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] -6 -6 -6
[2,] -6 -6 -6
Here in the above code, the elements of diff matrix are the subtraction of the corresponding elements of B and C through nested for loops. Using '-' operator for matrix subtraction: Similarly, the following R script uses the in-built operator '-':
Python
# R program for matrix addition
# using '-' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B - C)
Output:
[,1] [,2] [,3]
[1,] -1+0i 5.3+0i 0+0i
[2,] 2+3i 0.0+0i 0+0i
Properties of Matrix Subtraction:
- Non-Commutative: B - C != C - B
- Non-Associative: For n number of matrices A - (B - C) != (A - B) - C
- Order of the matrices involved must be same.
Matrices Multiplication
The multiplication of two same ordered matrices M_r_*_c and N_r_*_c yields a matrix R_r_*_c where every element is the product of corresponding elements of the input matrices.
Python
# R program to multiply two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
prod = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating product of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
prod[row, col] <- B[row, col] * C[row, col]
}
}
# Printing resultant matrix
print(prod)
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 7 27 55
[2,] 16 40 72
The elements of sum are the multiplication of the corresponding elements of B and C through nested for loops. Using '*' operator for matrix multiplication: Similarly, the following R script uses the in-built operator *:
Python
# R program for matrix multiplication
# using '*' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 1i, 0.1), nrow = 1, ncol = 3)
# Printing the resultant matrix
print (B * C)
Output:
[,1] [,2] [,3]
[1,] 2+0i -3+2i 0.54+0i
Properties of Matrix Multiplication:
- Commutative: B * C = C * B
- Associative: For n number of matrices A * (B * C) = (A * B) * C
- Order of the matrices involved must be same.
Matrices Division
The division of two same ordered matrices M_r_*_c and N_r_*_c yields a matrix R_r_*_c where every element is the quotient of corresponding elements of the first matrix element divided by the second.
Python
# R program to divide two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
div = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating product of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
div[row, col] <- B[row, col] / C[row, col]
}
}
# Printing resultant matrix
print(div)
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 0.1428571 0.3333333 0.4545455
[2,] 0.2500000 0.4000000 0.5000000
The elements of div matrix are the division of the corresponding elements of B and C through nested for loops. Using '/' operator for matrix division: Similarly, the following R script uses the in-built operator /:
Python
# R program for matrix division
# using '/' operator
# Creating 1st Matrix
B = matrix(c(4, 6i, -1), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 2i, 0), nrow = 1, ncol = 3)
# Printing the resultant matrix
print (B / C)
Output:
[,1] [,2] [,3]
[1,] 2+0i 3+0i -Inf+NaNi
Properties of Matrix Division:
- Non-Commutative: B / C != C / B
- Non-Associative: For n number of matrices A / (B / C) != (A / B) / C
- Order of the matrices involved must be same.
Note: Time Complexity of all the matrix operations = O(r*c) where r*c is the order of the matrix.
Similar Reads
Elementary Operations on Matrices
Elementary Operations on Matrices are the operations performed on the rows and columns of the matrix that do not change the value of the matrix. A matrix is a way of representing numbers in the form of an array, i.e. the numbers are arranged in the form of rows and columns. In a matrix, the rows and
9 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 column
8 min read
Matrix Operations
Matrix Operations are basic calculations performed on matrices to solve problems or manipulate their structure. Common operations include:Addition: Add two matrices of the same size.Subtraction: Subtract two matrices of the same size.Scalar Multiplication: Multiply each element of a matrix by a cons
8 min read
DataFrame Operations in R
DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as mattresses where each column of
9 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
Practice Questions on Matrices
Mastering matrices is crucial for anyone studying advanced mathematics, computer science, or engineering. This article provides a comprehensive set of practice questions on matrices will guide you through a variety of problems, from basic to advanced levels these problems designed to strengthen your
9 min read
Subtraction of Matrices
Subtraction of matrices is the addition of the negative of a matrix to another matrix which means A - B = A + (-B). The subtraction of the matrix is subtracting the corresponding row-column element of one matrix with the same row-column element of another matrix.Matrix subtraction is an operation wh
6 min read
Operations on Vectors in R
Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according t
4 min read
Operations on Lists in R Programming
Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element. A list is generated using list() function. It is basically a generic vector that contains different objects. R
4 min read
numpy matrix operations | eye() function
numpy.matlib.eye() is another function for doing matrix operations in numpy. It returns a matrix with ones on the diagonal and zeros elsewhere. Syntax : numpy.matlib.eye(n, M=None, k=0, dtype='float', order='C') Parameters : n : [int] Number of rows in the output matrix. M : [int, optional] Number o
2 min read