Module 6. Assignment- Creating basic and diagonal matrices

 

Adding two matrices combines their entries element by element, while subtraction takes the difference the same way. This makes it easy to see how corresponding values from  A and B interact.



The diag() function builds a matrix with the given numbers along the main diagonal. All off-diagonal entries are set to zero automatically.


Combining a special first column with a diagonal block using cbind() and rbind(). This shows how smaller pieces can be stacked together to form a structured matrix.


R code pasted:

#1. Matrix Addition & Subtraction

# Define matrices

A <- matrix(c(2, 0, 1, 3), ncol = 2)

B <- matrix(c(5, 2, 4, -1), ncol = 2)


# Addition

A_plus_B <- A + B

A_plus_B


# Subtraction

A_minus_B <- A - B

A_minus_B


#2. Build 4x4 diagonal matrix

D <- diag(c(4, 1, 2, 3))

D


#3. Construct a Custom 5×5 Matrix

first_col <- c(3, 2, 2, 2, 2)

diag_block <- diag(3, 4)


#Bind them together

M <- cbind(first_col, rbind(rep(1, 4), diag_block))

M

Comments

Popular posts from this blog

R Programming Journal – Christine Jacob

Module 2. Assignment: Function Debugging and Evaluation in R

Module 8. Assignment CSV files