Calculates the rank of a matrix.