Module # 6 Doing math in R part 2

 Module # 6 Doing math in R part 2


Answer the following questions and post your answer on your blog:
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





ncol = 2 means that numbers will be arranged into 2 columns in a 2x2 matrix.

When performing A+B, it would look like this:

(2+5) (1+4)        =  7  5
(0+2) (3+(-1))    =  2  2

When performing A - B, it would look like this:

 (2-5)    (1-4)       =  -3  -3
 (0-2)    (3-(-1))   =  -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.



With diag(c(4,1,2,3), we get a 4x4 matrix. These number would be placed in the diagonal elements. 



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
Hint: You can use the command diag() to build it.




diag(3,5,5) creates a 5x5 matrix with 3 in the diagonal.

col1 defines the first column as 3, 2, 2, 2, 2

row1 defines the first row as 3, 1, 1, 1, 1

With the combination of all these things, we get the desired matrix. 





My github

or 

https://github.com/AlenjaksAbraham/LIS4370.git


To see the code look under Module 6.R. 






Comments

Popular posts from this blog

Module # 7 R Object: S3 vs. S4 assignment

Module # 10 Building your own R package

Module # 9 Visualization in R