Module 6: Doing math with R. Part 2
Hello this week we will be continuing our exploration of different mathematical functions we can do using R.
Our assignment this time are the following tasks :
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
b) Find A - B
2. Using the diag() function to build a matrix of size 4 with the following values in the diagonal 4,1,2,3.
3. Generate the following matrix:
## [,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
Seems simple enough let us see what we can do!
Task #1:
Our first task is to do some simple linear algebra with two matrices. We are given their properties so first we simply input these matrices into R with the following lines of code:
> A <- matrix(c(2,0,1,3),ncol = 2)
> B <- matrix(c(5,2,4,-1),ncol = 2)
Awesome, now in order to find the product of A + B, all we have to do is simply apply the following line of code:
> A + B
Result being:
[,1] [,2]
[1,] 7 5
[2,] 2 2
We do essentially the same for A- B:
> A - B
and we get:
[,1] [,2]
[1,] -3 -3
[2,] -2 4
That was easy!
Task #2:
For our next task we need to produce a 4x4 matrix with the values 4,1,2,3 diagonally in the matrix([1,1],[2,2],[3,3],[4,4]) using the diag() function.
diag() is an interesting function, like much of R's functions it does many different things depending on how you use it. For instance if we just simply put a a scalar number as an argument it will return a square index matrix of the size of the number. In our case we want a diagonal matrix with specific values. To do this we can simply input a vector with those values as a sole argument in diag().
> diag(c(4,1,2,3))
This produces the following matrix:
[,1] [,2] [,3] [,4]
[1,] 4 0 0 0
[2,] 0 1 0 0
[3,] 0 0 2 0
[4,] 0 0 0 3
We don't need to specify any dimensions of this matrix, R will automatically assume we just want a 4x4 matrix based on the length of the vector we used as an argument.
Task #3:
Finally for our last task we need to generate the following matrix.
## [,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
There are a few different approaches to this. Here is what I decided to do.
First we used diag() to created a a 5x5 matrix containing 3s in the diagonal position:
> m <- diag(3,nrow = 5)
Using diag() we assign the matrix to variable m. The first argument is a pseudo scalar value of 3. (R views this similar to c(3)). The second argument tells R that we want this matrix to have 5 rows. If we don't add this argument, R will produce a 3x3 index matrix. The result of our Matrix m is currently:
[,1] [,2] [,3] [,4] [,5]
[1,] 3 0 0 0 0
[2,] 0 3 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 3 0
[5,] 0 0 0 0 3
Next we need to add the 2s and 1s to the matrix.
I decided to add the necessary values to the desired indices making sure to avoid [1,1]:
> m[2:5,1] <- 2
> m[2:5,1] <- 2
> m[1,2:5] <- 1
Our end result is our desired matrix:
[,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
Excellent!
Hope you enjoyed this weeks module! Feel free to contact me with any concerns.
-Anthony
Comments
Post a Comment