Matrix Operations and Diagonal Construction in R
# Question 1 # Define matrices A and B A <- matrix ( c ( 2 , 0 , 1 , 3 ), ncol = 2 ) B <- matrix ( c ( 5 , 2 , 4 , - 1 ), ncol = 2 ) # a) A + B A_plus_B <- A + B # b) A - B A_minus_B <- A - B A B A_plus_B A_minus_B # Question 2 # Create diagonal matrix with values 4,1,2,3 D <- diag ( c ( 4 , 1 , 2 , 3 )) D # Question 3 # Generate the required 5x5 matrix M <- diag ( 3 , 5 ) M [ 2 : 5 , 1 ] <- 2 M [ 1 , 2 : 5 ] <- 1 M In this assignment, I practiced basic matrix operations in R, including matrix addition, subtraction, and constructing matrices using the diag() function. First, I defined matrices A and B using the matrix() function. I then computed A + B and A − B to demonstrate how R performs element-wise arithmetic operations on matrices of the same dimensions. This reinforces how R handles structured data efficiently using vectorized operations. Next, I used the diag() function to construct a 4×4 diagonal matrix with the values 4, ...