R provides several operators and functions for performing matrix arithmetic. Here are a few examples:
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Matrix Transpose: t()
- Matrix Multiplication: %*%
Here's an example code that demonstrates matrix addition and multiplication:
# Create two matrices
matrix1 <- matrix(1:4, nrow = 2)
matrix2 <- matrix(5:8, nrow = 2)
# Matrix addition
addition_result <- matrix1 + matrix2
# Matrix multiplication
multiplication_result <- matrix1 %*% matrix2
# Print the results
print(addition_result)
print(multiplication_result)
Output:
[,1] [,2]
[1,] 6 10
[2,] 8 12
[,1] [,2]
[1,] 19 22
[2,] 43 50