Assignment 6
Code:
# Task 1: Matrix Addition & Subtraction
A = matrix(c(2, 0, 1, 3), ncol = 2)
B = matrix(c(5, 2, 4, -1), ncol = 2)
cat("Matrix A:\n")
print(A)
cat("\nMatrix B:\n")
print(B)
A_plus_B = A + B
cat("\nA + B:\n")
print(A_plus_B)
A_minus_B = A - B
cat("\nA - B:\n")
print(A_minus_B)
# Task 2: Create a Diagonal Matrix
D = diag(c(4, 1, 2, 3))
cat("\nDiagonal Matrix D:\n")
print(D)
# Task 3: Construct a Custom 5x5 Matrix
first_col = c(3, 2, 2, 2, 2)
remaining_block = rbind(
c(1, 1, 1, 1),
diag(3, 4)
)
custom_matrix = cbind(first_col, remaining_block)
cat("\nCustom 5x5 Matrix:\n")
print(custom_matrix)
Output:
> # Task 1: Matrix Addition & Subtraction
> A = matrix(c(2, 0, 1, 3), ncol = 2)
> B = matrix(c(5, 2, 4, -1), ncol = 2)
> cat("Matrix A:\n")
Matrix A:
> print(A)
[,1] [,2]
[1,] 2 1
[2,] 0 3
> cat("\nMatrix B:\n")
Matrix B:
> print(B)
[,1] [,2]
[1,] 5 4
[2,] 2 -1
> A_plus_B = A + B
> cat("\nA + B:\n")
A + B:
> print(A_plus_B)
[,1] [,2]
[1,] 7 5
[2,] 2 2
> A_minus_B = A - B
> cat("\nA - B:\n")
A - B:
> print(A_minus_B)
[,1] [,2]
[1,] -3 -3
[2,] -2 4
> # Task 2: Create a Diagonal Matrix
> D = diag(c(4, 1, 2, 3))
> cat("\nDiagonal Matrix D:\n")
Diagonal Matrix D:
> print(D)
[,1] [,2] [,3] [,4]
[1,] 4 0 0 0
[2,] 0 1 0 0
[3,] 0 0 2 0
[4,] 0 0 0 3
> # Task 3: Construct a Custom 5x5 Matrix
> first_col = c(3, 2, 2, 2, 2)
> remaining_block = rbind(
+ c(1, 1, 1, 1),
+ diag(3, 4)
+ )
> custom_matrix = cbind(first_col, remaining_block)
> cat("\nCustom 5x5 Matrix:\n")
Custom 5x5 Matrix:
> print(custom_matrix)
first_col
[1,] 3 1 1 1 1
[2,] 2 3 0 0 0
[3,] 2 0 3 0 0
[4,] 2 0 0 3 0
[5,] 2 0 0 0 3
>
Explanation:
- Task 1 Explanation: Matrix addition and subtraction work by adding or subtracting each element in the same position from both matrices. For this to work, both matrices need to have the exact same dimensions (same number of rows and columns).
- Task 2 Explanation: The
diag() function is a quick way to create a diagonal matrix where you specify the values that go down the main diagonal, and R automatically fills everything else with zeros. This is really useful for identity matrices and scaling operations.
- Task 3 Explanation: Instead of manually typing out all 25 numbers, I built this matrix by combining smaller pieces. I used
rbind() to stack rows together and cbind() to attach columns side-by-side, which makes it much easier to create structured matrices programmatically.
- Task 1 Explanation: Matrix addition and subtraction work by adding or subtracting each element in the same position from both matrices. For this to work, both matrices need to have the exact same dimensions (same number of rows and columns).
- Task 2 Explanation: The
diag()function is a quick way to create a diagonal matrix where you specify the values that go down the main diagonal, and R automatically fills everything else with zeros. This is really useful for identity matrices and scaling operations.
- Task 3 Explanation: Instead of manually typing out all 25 numbers, I built this matrix by combining smaller pieces. I used
rbind()to stack rows together andcbind()to attach columns side-by-side, which makes it much easier to create structured matrices programmatically.
Comments
Post a Comment