Operations on Matrices in R

28 Jun, 2022
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. 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.
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.
The addition of two same ordered matrices and yields a matrix where every element is the sum of corresponding elements of the input matrices. 
Python3
# R program to add two matrices # Creating 1st MatrixB = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3) # Getting number of rows and columnsnum_of_rows = nrow(B)num_of_cols = ncol(B) # Creating matrix to store resultssum = matrix(, nrow = num_of_rows, ncol = num_of_cols) # Printing Original matricesprint(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 +: 
Python3
# R program for matrix addition# using '+' operator # Creating 1st MatrixB = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3) # Printing the resultant matrixprint(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.
The subtraction of two same ordered matrices and yields a matrix where every element is the difference of corresponding elements of the second input matrix from the first. 
Python3
# R program to add two matrices # Creating 1st MatrixB = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3) # Getting number of rows and columnsnum_of_rows = nrow(B)num_of_cols = ncol(B) # Creating matrix to store resultsdiff = matrix(, nrow = num_of_rows, ncol = num_of_cols) # Printing Original matricesprint(B)print(C) # Calculating diff of matricesfor(row in 1:num_of_rows){    for(col in 1:num_of_cols)    {        diff[row, col] <- B[row, col] - C[row, col]    }} # Printing resultant matrixprint(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 ‘-‘: 
Python3
# R program for matrix addition# using '-' operator # Creating 1st MatrixB = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3) # Printing the resultant matrixprint(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.
The multiplication of two same ordered matrices and yields a matrix where every element is the product of corresponding elements of the input matrices. 
Python3
# R program to multiply two matrices # Creating 1st MatrixB = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3) # Getting number of rows and columnsnum_of_rows = nrow(B)num_of_cols = ncol(B) # Creating matrix to store resultsprod = matrix(, nrow = num_of_rows, ncol = num_of_cols) # Printing Original matricesprint(B)print(C) # Calculating product of matricesfor(row in 1:num_of_rows){    for(col in 1:num_of_cols)    {        prod[row, col] <- B[row, col] * C[row, col]    }} # Printing resultant matrixprint(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 *: 
Python3
# R program for matrix multiplication# using '*' operator # Creating 1st MatrixB = matrix(c(1, 2 + 3i, 5.4), nrow = 1, ncol = 3) # Creating 2nd MatrixC = matrix(c(2, 1i, 0.1), nrow = 1, ncol = 3) # Printing the resultant matrixprint (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.
The division of two same ordered matrices and yields a matrix where every element is the quotient of corresponding elements of the first matrix element divided by the second. 
Python3
# R program to divide two matrices # Creating 1st MatrixB = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) # Creating 2nd MatrixC = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3) # Getting number of rows and columnsnum_of_rows = nrow(B)num_of_cols = ncol(B) # Creating matrix to store resultsdiv = matrix(, nrow = num_of_rows, ncol = num_of_cols) # Printing Original matricesprint(B)print(C) # Calculating product of matricesfor(row in 1:num_of_rows){    for(col in 1:num_of_cols)    {        div[row, col] <- B[row, col] / C[row, col]    }} # Printing resultant matrixprint(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 /: 
Python3
# R program for matrix division# using '/' operator # Creating 1st MatrixB = matrix(c(4, 6i, -1), nrow = 1, ncol = 3) # Creating 2nd MatrixC = matrix(c(2, 2i, 0), nrow = 1, ncol = 3) # Printing the resultant matrixprint (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.
simmytarika5
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Printing Output of an R Program
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?