Module # 6 Doing math in R part 2
Github Link: https://github.com/rayhankhan-svg/r-programming-assignments R Code: # 1) Matrices A and B A <- matrix(c(2,0,1,3), ncol=2) B <- matrix(c(5,2,4,-1), ncol=2) A B # a) A + B A_plus_B <- A + B A_plus_B # b) A - B A_minus_B <- A - B A_minus_B # 2) diag() matrix size 4 with diagonal values 4,1,2,3 D <- diag(c(4,1,2,3)) D # 3) Generate the 5x5 matrix using diag() M <- diag(3, 5) # start with 3s on the diagonal M[1, 2:5] <- 1 # first row (cols 2-5) become 1 M[2:5, 1] <- 2 # first column (rows 2-5) become 2 M Output: > # 1) Matrices A and B > A <- matrix(c(2,0,1,3), ncol=2) > B <- matrix(c(5,2,4,-1), ncol=2) > > A [,1] [,2] [1,] 2 1 [2,] 0 3 > B [,1] [,2] [1,] 5 4 [2,] 2 -1 > > # a) A + B ...