An iterative method for computing generalized inverses