General Matrix Operations in R
Basic Arithmetic Operations
Matrices support element-wise arithmetic operations. For matrices of the same dimensions, you can perform addition, subtraction, multiplication, and division directly:
Addition and Subtraction
# Example 1: Matrix addition and subtraction m1 <- matrix(1:4, nrow = 2) m2 <- matrix(5:8, nrow = 2) # Addition sum_m <- m1 + m2 print(sum_m) # Subtraction diff_m <- m1 - m2 print(diff_m) #The results are: # Addition # [,1] [,2] # [1,] 6 8 # [2,] 8 10 # Subtraction # [,1] [,2] # [1,] -4 -4 # [2,] -4 -4
Element-wise Multiplication and Division
# Example 2: Element-wise multiplication and division elem_mult <- m1 * m2 print(elem_mult) elem_div <- m1 / m2 print(elem_div) # The results are: # Multiplication # [,1] [,2] # [1,] 5 12 # [2,] 12 24 # Division # [,1] [,2] # [1,] 0.2 0.5 # [2,] 0.5 0.5
Matrix Multiplication
Matrix multiplication (also known as matrix product) is performed using the %*% operator. The number of columns in the first matrix must equal the number of rows in the second matrix:
# Example 3: Matrix multiplication m1 <- matrix(1:4, nrow = 2) m2 <- matrix(5:8, nrow = 2) # Matrix multiplication mat_mult <- m1 %*% m2 print(mat_mult) # The result is: # [,1] [,2] # [1,] 19 22 # [2,] 43 50
Matrix Transposition
The transpose of a matrix is obtained using the t() function, which swaps rows and columns:
# Example 4: Transposing a matrix m <- matrix(1:6, nrow = 2) m_transpose <- t(m) print(m_transpose) # The result is: # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 2 4 6
Matrix Determinant and Inverse
Determinant
The determinant of a matrix is computed using the det() function. This is applicable only to square matrices:
# Example 5: Calculating the determinant m <- matrix(c(1, 2, 3, 4), nrow = 2) det_m <- det(m) print(det_m) # The result is: # diff # -
Inverse
The inverse of a matrix (if it exists) is computed using the solve() function:
# Example 6: Calculating the inverse inv_m <- solve(m) print(inv_m) # The result is: # [,1] [,2] # [1,] -2.0 1.0 # [2,] 1.5 -0.5
Eigenvalues and Eigenvectors
To compute the eigenvalues and eigenvectors of a matrix, use the eigen() function:
# Example 7: Eigenvalues and eigenvectors eigen_decomp <- eigen(m) print(eigen_decomp$values) # Eigenvalues print(eigen_decomp$vectors) # Eigenvectors # The output includes eigenvalues and eigenvectors: # $values # [1] 5.372281 0.627719 # $vectors # [,1] [,2] # [1,] -0.8245648 -0.4159736 # [2,] -0.5657675 0.9093760
Matrix Norms
Matrix norms provide a measure of the size or length of a matrix. Common norms include the Frobenius norm and the infinity norm.
Frobenius Norm
The Frobenius norm is computed using the norm() function with type = “F”:
# Example 8: Frobenius norm frobenius_norm <- norm(m, type = "F") print(frobenius_norm)
Infinity Norm
The infinity norm (maximum row sum) is computed using norm() with type = “I”:
# Example 9: Infinity norm infinity_norm <- norm(m, type = "I") print(infinity_norm)
Summary of Key Matrix Operations
- Addition and Subtraction: +, –
- Element-wise Multiplication and Division: *, /
- Matrix Multiplication: %*%
- Transposition: t()
- Determinant: det()
- Inverse: solve()
- Eigenvalues and Eigenvectors: eigen()
- Matrix Norms: norm()
Understanding these operations allows you to perform a wide range of calculations and manipulations with matrices in R.