Module 6: Matrix Operations in R
In this assignment, I practiced basic matrix operations in R, including
addition, subtraction, and creating special matrices using the diag()
function. These skills are important for understanding linear algebra
concepts in data analysis and statistics. The topics are connected to
matrix operations discussed in The Art of R Programming and good coding practices from R Packages.
Question 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
First, I created two matrices:
Matrix addition and subtraction are done element-by-element, as long as both matrices have the same dimensions.
Question 2: Using the diag() function to build a matrix of size 4 with the following values in the diagonal 4,1,2,3.
Next, I created a 4×4 matrix with values 4, 1, 2, and 3 on the diagonal.
The diag() function places the given values on the main diagonal and fills the rest with zeros.
Question 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
This matrix was created by starting with a diagonal matrix and then modifying the first row and column.
Overall, this assignment helped me strengthen my understanding of matrix operations in R and showed how basic linear algebra concepts can be applied to real data analysis tasks.
Comments
Post a Comment