Doing Math on Matrices
I used the following values to find the inverse and determinants of two matrices.
A = matrix(1:100, nrow=10)
B = matrix(1:1000, nrow=10)
First, I assigned the matrices in R.
A <- matrix(1:100, nrow=10)
B <- matrix(1:1000, nrow=10)
To obtain the determinant of each matrix, I used the det function.
det(A)
Result:
0
det(B)
Result:
Error in determinant.matrix(x, logarithm = TRUE, ...) :
'x' must be a square matrix
Then, to obtain the inverse of each matrix, I used the solve function.
solve(A)
Result:
Error in solve.default(A) :
Lapack routine dgesv: system is exactly singular: U[3,3] = 0
solve(B)
Result:
Error in solve.default(B) : 'a' (10 x 100) must be square
Looking at the matrices, we can see that A is a square matrix because it has the same number of rows and columns. There are 100 values in the matrix; dividing that by 10 rows gives me 10 columns. This makes matrix A a 10x10 square matrix. With B, there are 1000 values in the matrix, and still 10 rows, so there are 100 columns. Matrix B is a 10x100 matrix, so it is not a square.
It is important to know this before any calculations because non-square matrices do not have a determinant or an inverse. This is why the det and solve functions for B result in an error, saying it must be a square.
It is also important to solve for the determinant of a square matrix first before the inverse, because if the determinant is zero, then that tells you the matrix will not have an inverse. For example, the det(A) function calculates matrix A having a zero determinant. Then, when I used solve(A), I received that error. The error occurs because the matrix is singular; its determinant equals zero, so there is no inverse.
Comments
Post a Comment