Assignment 6
Assignment 6: Matrix Operations and Construction
In this assignment, I practiced creating and manipulating matrices in R. The goal was to strengthen my understanding of how R handles matrix operations such as addition, subtraction, diagonal construction, and combining blocks of matrices to form structured arrays.
R Code
🔹 Task 1: Matrix Addition & Subtraction
# Task 1: define A and B
A <- matrix(c(2, 0, 1, 3), ncol = 2)
B <- matrix(c(5, 2, 4, -1), ncol = 2)
# Addition
A_plus_B <- A + B
print("A + B:")
print(A_plus_B)
# Subtraction
A_minus_B <- A - B
print("A - B:")
print(A_minus_B)
🔹 Task 2: Create a Diagonal Matrix
# Task 2: Create a Diagonal Matrix
D <- diag(c(4, 1, 2, 3))
print("Diagonal Matrix D:")
print(D)
🔹 Task 3: Construct a Custom 5×5 Matrix
# Task 3: Construct a Custom 5x5 Matrix
# Step 1: create the first row
row1 <- c(3, 1, 1, 1, 1)
# Step 2: create the remaining rows:
# - first column all 2’s (rows 2–5)
# - diagonal of 3’s starting from row 2
block <- diag(3, 4) # 4x4 identity scaled by 3
col_block <- matrix(2, nrow = 4, ncol = 1)
rest <- cbind(col_block, block)
# Step 3: combine row 1 with the rest
M <- rbind(row1, rest)
print("Custom 5x5 Matrix M:")
print(M)
Output
Matrix Addition & Subtraction: For addition and subtraction, R performs element-by-element operations. Each corresponding entry in matrix A and matrix B is added or subtracted to produce a new matrix of the same dimensions.
Diagonal Matrix (diag()): The diag() function creates a square matrix with specified values on the diagonal and zeros elsewhere. This is useful for creating identity matrices or scaling diagonal elements.
Custom 5×5 Matrix Construction: This matrix is built by combining a custom first row with a block structure. It uses a column of repeated values and a diagonal matrix to fill the rest.
Conclusion
diag() helped me see how R efficiently represents data along specific axes, while building a custom 5×5 matrix reinforced how rbind() and cbind() can be used to combine and organize matrix components programmatically.
Comments
Post a Comment