A Direct Method for Generalized Matrix Inversion