Assignment #6 - Matrix Operations and Construction
Full R code used for tasks 1-3:
The printed output or matrices from each task:
Summaries of each operation:
1. Matrix addition and subtraction:
The "A + B" function adds the elements of matrices A and B, and the "A - B" function subtracts the elements of matrix B from A. Both of these functions result in a new matrix of the same size.
2. Create a diagonal matrix:
The "diag(c(4, 1, 2, 3))" function creates a 4x4 matrix with the indicated numbers along the diagonal and places zeros in each of the remaining positions.
3. Construct a custom 5x5 matrix:
The "D <- diag(3, 5)" line creates a 5x5 matrix with the number 3 along the diagonal and zeros in the remaining positions. Then the "D[, 1] <- c(3, 2, 2, 2, 2)" line retains the first value, but replaces each value in column 1 to be the number 2. Finally, "D[1, 2:5] <- 1" creates the first row pattern to be (3, 1, 1, 1, 1), and completes the custom matrix.
Comments
Post a Comment