Assignment #6
In this module I practiced basic matrix math in R. I created two matrices, A and B, using the matrix function. Since R fills matrices by column, I checked how they looked first and then did the operations. To find A + B and A − B, I just used A + B and A - B in R. Matrix addition and subtraction are done element by element, so each number lines up with the same position in the other matrix. R printed the results correctly for both operations.
Next, I used the diag() function to build a 4x4 diagonal matrix with the values 4, 1, 2, and 3 on the diagonal. A diagonal matrix has zeros everywhere except the diagonal from top left to bottom right. I created it with diag(c(4,1,2,3), 4, 4) and the output matched what the question asked for.
For the last question, I had to generate a specific 5x5 matrix. I started with a diagonal matrix of 3s using diag(3, 5). Then I edited the first column to be 3, 2, 2, 2, 2 and changed the first row to 3, 1, 1, 1, 1. After those small edits, the matrix matched the one in the assignment exactly. This part helped me understand how to build a matrix step-by-step in R instead of typing everything manually.
macauleywilson/r-programming-assignments



Comments
Post a Comment