Monday, February 20, 2023

Assignment 6

 Assignment 6

The code for this assignment may be accessed at https://github.com/Wellis11/assignment6.

1. Consider A=matrix(c(2,0,1,3), ncol=2) and B=matrix(c(5,2,4,-1), ncol=2).
a) Find A + B

For this part of the assignment I just created the matrices as objects and added them together.  This worked because the matrices had the same dimensions.

     [,1] [,2]
[1,]    7    5
[2,]    2    2

b) Find A - B

For this part of the assignment I just created the matrices as objects and subtracted B from A.  This worked because the matrices had the same dimensions.

     [,1] [,2]
[1,]   -3   -3
[2,]   -2    4

2. Using the diag() function to build a matrix of size 4 with the following values in the diagonal 4,1,2,3.

For this part of the assignment, I used the code:

m=c(4,1,2,3) #create vector with specified values

M=diag(m,4) #create matrix with vector values in diagonal

print(M)

which gave this output:

     [,1] [,2] [,3] [,4]
[1,]    4    0    0    0
[2,]    0    1    0    0
[3,]    0    0    2    0
[4,]    0    0    0    3

3. Generate the following matrix:

I did this last part in three sections.  I created 5x5 matix with 3 as the diagonal.  I then created 2 separate 5x5 matrices with the numbers needed to contribute to the final product via addition.  Here is the code and the output:

N=(diag(5))*3

O=matrix(c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0), ncol=5)

P=matrix(c(0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), ncol=5)

Q=N+O+P

print(Q)

     [,1] [,2] [,3] [,4] [,5]
[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

No comments: