Open In App

Operations on Matrices in R

Last Updated : 09 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R, matrices are two-dimensional arrays of data numeric, logical or complex organized in 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 is defined by its order:

Order = number of rows × number of columns

Example:

R
M = matrix(1:9, nrow = 3, ncol = 3)
print(M)

Output
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

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.

Matrix Addition

Matrix addition is the process of adding corresponding elements from two matrices. This operation is only defined when the two matrices have the same dimensions (i.e., same number of rows and columns).

Method 1: Using Nested Loops

This loop adds each element of B and C and stores the result in sum_matrix.

R
# Input Matrices
A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
B <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)

print(A)
print(B)

# Initialize result matrix
sum_matrix <- matrix(0, nrow = nrow(A), ncol = ncol(B))

# Element-wise addition
for (i in 1:nrow(A)) {
  for (j in 1:ncol(A)) {
    sum_matrix[i, j] <- A[i, j] + B[i, j]
  }
}

print(sum_matrix)

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

Explanation:

  • A and B are both 2x3 matrices.
  • We iterate through each element using nested loops and add corresponding elements.
  • The result is stored in sum_matrix.

Method 2: Using the + Operator

R
A <- matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), 2, 3)
B <- matrix(c(2, 0i, 0.1, 3, 4, 5), 2, 3)

print(A)
print(B)
print(A + B)

Output
     [,1]   [,2] [,3]
[1,] 1+0i 5.4+0i 4+0i
[2,] 2+3i 3.0+0i 5+0i
     [,1]   [,2] [,3]
[1,] 2+0i 0.1+0i 4+0i
[2,] 0+0i 3.0+0i 5+0i
     [,1]   [,2]  [,3]
[1,] 3+0i 5.5+0i  8+0i
[2,] 2+3i 6.0+0i 10+0i...

Explanation:

  • The + operator simplifies the process and performs element-wise addition automatically.
  • If any value is complex, R returns the result in complex form for the entire matrix.

Properties of Matrix Addition:

  • Commutative: A + B = B + A
  • Associative: A + (B + C) = (A + B) + C
  • Defined only when: The matrices have same dimensions

Matrices Subtraction

Matrix subtraction follows the same structural rules as addition. It involves subtracting corresponding elements from the first matrix by the second matrix.

1. Using Loops

R
A <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3)
B <- matrix(c(7, 8, 9, 10, 11, 12), 2, 3)

print(A)
print(B)
diff <- matrix(, 2, 3)

for (i in 1:2) {
  for (j in 1:3) {
    diff[i, j] <- A[i, j] - B[i, j]
  }
}

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 A and B through nested for loops.

2. Using the - Operator

Using '-' operator for matrix subtraction

R
A <- matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), 2, 3)
B <- matrix(c(2, 0i, 0.1, 3, 4, 5), 2, 3)

print(A)
print(B)
print(A - B)

Output
     [,1]   [,2] [,3]
[1,] 1+0i 5.4+0i 4+0i
[2,] 2+3i 3.0+0i 5+0i
     [,1]   [,2] [,3]
[1,] 2+0i 0.1+0i 4+0i
[2,] 0+0i 3.0+0i 5+0i
      [,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: A - B ≠ B - A
  • Non-Associative: A - (B - C) ≠ (A - B) - C
  • Same order required

Matrix Multiplication (Element-wise)

In element-wise multiplication, each element in one matrix is multiplied by the corresponding element in the second matrix. This is different from matrix product (dot product).

1. Using Loops

R
A <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3)
B <- matrix(c(7, 8, 9, 10, 11, 12), 2, 3)

print(A)
print(B)

prod <- matrix(, 2, 3)

for (i in 1:2) {
  for (j in 1:3) {
    prod[i, j] <- A[i, j] * B[i, j]
  }
}

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 A and B through nested for loops.

2. Using * Operator

Using '*' operator for matrix multiplication 

R
# Define 2x2 real number matrices
A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)

# Element-wise multiplication
result <- A * B

print(A)
print(B)
print(result)

Output
     [,1] [,2]
[1,]    1    3
[2,]    2    4
     [,1] [,2]
[1,]    5    7
[2,]    6    8
     [,1] [,2]
[1,]    5   21
[2,]   12   32

Properties of Matrix Multiplication:

  • Commutative: A * B = B * A
  • Associative: A * (B * C) = (A * B) * C
  • Order of the matrices involved must be same.

Matrix Division (Element-wise)

Element-wise division divides each element of one matrix by the corresponding element in another matrix.

1. Using Loops

R
A <- matrix(c(1, 2, 3, 4, 5, 6), 2, 3)
B <- matrix(c(7, 8, 9, 10, 11, 12), 2, 3)

print(A)
print(B)

div <- matrix(, 2, 3)

for (i in 1:2) {
  for (j in 1:3) {
    div[i, j] <- A[i, j] / B[i, j]
  }
}

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.250...

The elements of div matrix are the division of the corresponding elements of B and C through nested for loops.

2. Using / Operator

Using '/' operator for matrix division

R
# Define 2x2 real number matrices
A <- matrix(c(10, 20, 30, 40), nrow = 2)
B <- matrix(c(2, 4, 5, 8), nrow = 2)

# Element-wise division
result <- A / B

print(A)
print(B)
print(result)

Output
     [,1] [,2]
[1,]   10   30
[2,]   20   40
     [,1] [,2]
[1,]    2    5
[2,]    4    8
     [,1] [,2]
[1,]    5    6
[2,]    5    5

Properties of Matrix Division: 

  • Non-Commutative: A / B ≠ B / A
  • Non-Associative: A / (B / C) ≠ (A / B) / C
  • Same order required

Next Article
Practice Tags :

Similar Reads