An iterative method for computing the generalized inverse of a matrix