Module 5. Assignment- inspecting matrices in R
(Please select image for better clarity)
- Why
solve(A)anddet(A)work.
For matrix A, det(A) gives an output of zero. That’s because of how A <- matrix(1:100, nrow=10) is filled: the columns are simple combinations of the vectors of all 1s and 1:10, so the matrix doesn’t have full rank. A zero determinant means A is singular, which means there’s no true inverse. That’s why solve(A) would normally error out. It works in the script because it is wrapped with tryCatch to catch the error.
- Why operations on B fail (non‑square matrix).
Matrix B it’s 10×100, not square which is simply why it failed. Both solve() and det() need a square matrix. So the unwrapped calls would error by definition (as seen), and with tryCatch it captures those errors and continues with the computation.
- Any notes on numeric stability or performance.
I keep it simple: first I check dim() to make sure the matrix is square. Then I wrap the risky parts—solve() and det()—with tryCatch so if something goes wrong (non-square or singular) it doesn’t break the script. If the goal is , I use solve(A, b) instead of computing the full inverse. For determinants, I just use the result I already stored (like detA) and, when I need details, I read detA$modulus and detA$sign to get the numeric/log-det view. This keeps the code clean, avoids crashes, and saves time by not repeating heavy computations.
Code can be found below in Module 5 folder:
Comments
Post a Comment