R Programming Journal Module 6 - Wesley Huang
- Wesley Huang
- Feb 23
- 1 min read
In this assignment, I explored matrix operations in R, including matrix addition, subtraction, diagonal, matrices, and matrix construction using the diag() function.
First, I created two matrices A and B using the matrix() function. Each matrix was a 2 × 2 matrix constructed from given values. I then calculated the sum (A + B) and difference (A − B) using basic matrix arithmetic. R performs these operations element-by-element as long as the matrices have the same dimensions.
Next, I used the diag() function to create a diagonal matrix of size 4 with values 4, 1, 2, and 3 along the main diagonal. All non-diagonal elements were automatically set to zero, demonstrating how diagonal matrices are structured.
Finally, I generated a 5 × 5 matrix with specific values. I first created a matrix with 3s on the diagonal using diag(3, 5), then replaced the first column with custom values to match the required structure. This showed how matrices can be modified after creation.
Overall, this assignment reinforced key concepts in matrix manipulation, including arithmetic operations, diagonal matrices, and customizing matrix entries in R.
Below I have included my input and output from R.




Comments