Operations on Matrices in detail
Matrices in R consist of values, either real or complex numbers, arranged in a structured format with a fixed number of rows and columns. They are essential for representing data in an organized manner. Elements in a matrix should be enclosed in brackets or parentheses.
For example, a matrix with 9 elements is shown below:
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
This matrix A has 3 rows and 3 columns. Each element can be identified by its row and column index. For instance, a[2,3] = 6.
The order of a matrix is determined by the number of rows and columns:
Order = Number of rows × Number of columns
So, matrix A has an order of 3 × 3.
Operations on Matrices
The fundamental matrix operations include Addition, Subtraction, Multiplication, and Division. Both matrices must have the same dimensions for these operations to be performed.
1. Matrix Addition: The sum of two matrices of the same order results in another matrix where each element is the sum of corresponding elements.
R Code for Matrix Addition
# Creating First Matrix
M1 = matrix(c(2, 4, 6, 8, 10, 12), nrow = 2, ncol = 3)
# Creating Second Matrix
M2 = matrix(c(1, 3, 5, 7, 9, 11), nrow = 2, ncol = 3)
# Performing Addition
result = M1 + M2
# Printing Result
print(result)
Output:
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 5 11 17
2. Matrix Subtraction
Matrix subtraction works similarly, where each element of the second matrix is subtracted from the corresponding element of the first.
R Code for Matrix Subtraction
# Creating Matrices
M1 = matrix(c(5, 10, 15, 20, 25, 30), nrow = 2, ncol = 3)
M2 = matrix(c(2, 4, 6, 8, 10, 12), nrow = 2, ncol = 3)
# Performing Subtraction
result = M1 - M2
# Printing Result
print(result)
Output:
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 12 15 18
3. Matrix Multiplication: Matrix multiplication involves multiplying corresponding elements when matrices have the same dimensions.
R Code for Matrix Multiplication
# Creating Matrices
M1 = matrix(c(2, 3, 4, 5, 6, 7), nrow = 2, ncol = 3)
M2 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Element-wise Multiplication
result = M1 * M2
# Printing Result
print(result)
Output:
[,1] [,2] [,3]
[1,] 2 6 12
[2,] 20 30 42
4. Matrix Division: Each element of the first matrix is divided by the corresponding element of the second matrix.
R Code for Matrix Division
# Creating Matrices
M1 = matrix(c(10, 20, 30, 40, 50, 60), nrow = 2, ncol = 3)
M2 = matrix(c(2, 4, 6, 8, 10, 12), nrow = 2, ncol = 3)
# Element-wise Division
result = M1 / M2
# Printing Result
print(result)
Output:
[,1] [,2] [,3]
[1,] 5.0 5.0 5.0
[2,] 5.0 5.0 5.0
Properties of Matrix Operations
Matrix Addition Properties:
- Commutative: M1 + M2 = M2 + M1
- Associative: M1 + (M2 + M3) = (M1 + M2) + M3
- Matrices must have the same dimensions.
Matrix Subtraction Properties:
- Non-Commutative: M1 – M2 ≠ M2 – M1
- Non-Associative: M1 – (M2 – M3) ≠ (M1 – M2) – M3
- Matrices must have the same dimensions.
Matrix Multiplication Properties:
- Commutative: M1 * M2 = M2 * M1
- Associative: M1 * (M2 * M3) = (M1 * M2) * M3
- Matrices must have the same dimensions.
Matrix Division Properties:
- Non-Commutative: M1 / M2 ≠ M2 / M1
- Non-Associative: M1 / (M2 / M3) ≠ (M1 / M2) / M3
- Matrices must have the same dimensions.
Leave a Reply