How to get rank of a matrix in eigen?
You need to convert your matrix to a rank-revealing decomposition. For instance FullPivLU
. If you have a matrix3f
it looks like this :
FullPivLU<Matrix3f> lu_decomp(your_matrix);
auto rank = lu_decomp.rank();
Edit
Decomposing the matrix is the most common way to get the rank. Although, LU is not the most reliable way to achieve it for floating values as explained on the rank article on wikipedia
When applied to floating point computations on computers, basic Gaussian elimination (LU decomposition) can be unreliable, and a rank-revealing decomposition should be used instead. An effective alternative is the singular value decomposition (SVD), but there are other less expensive choices, such as QR decomposition with pivoting (so-called rank-revealing QR factorization), which are still more numerically robust than Gaussian elimination. Numerical determination of rank requires a criterion for deciding when a value, such as a singular value from the SVD, should be treated as zero, a practical choice which depends on both the matrix and the application.
So you might get more accurate results with Eigen::ColPivHouseholderQR< MatrixType >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With