By extending the well-known Jacobi and Gauss-Seidel iterations for
, the iterative solutions of matrix equations
and generalized Sylvester matrix equations
(including the Sylvester equation
as a special case) are studied, and a gradient based and a least-squares based iterative algorithms for the solution is proved. It is proved that the iterative solution always converges to the exact solution for any initial values. The basic idea is to regard the unknown matrix
to be solved as the parameters of a system to be identified, and to obtain the iterative solutions by applying the hierarchical identification principle. Finally, the algorithms are tested and their effectiveness is shown using a numerical example.