If I have the matrix W size (4 x 4)
W = Array[Subscript[a, ##] &, {4, 4}]
$W=\left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right)$
I use this code
With[{inds = DeleteCases[Range@Length@W, 2]},
Table[W[[i, j]] + If[i != j, 1, 0] W[[i, 2]] W[[2, j]], {i,
inds}, {j, inds}]]
% // MatrixForm
$F=\left( \begin{array}{ccc} a_{1,1} & a_{1,3}+a_{1,2} a_{2,3} & a_{1,4}+a_{1,2} a_{2,4} \\ a_{3,1}+a_{2,1} a_{3,2} & a_{3,3} & a_{2,4} a_{3,2}+a_{3,4} \\ a_{4,1}+a_{2,1} a_{4,2} & a_{2,3} a_{4,2}+a_{4,3} & a_{4,4} \\ \end{array} \right)$
I need to repeat this code again to get a 2x2 matrix
With[{inds = DeleteCases[Range@Length@F, 2]},
Table[F[[i, j]] + If[i != j, 1, 0] F[[i, 2]] F[[2, j]], {i,
inds}, {j, inds}]]
% // MatrixForm
$\scriptsize D=\left( \begin{array}{cc}a_{1,1} & a_{1,4}+a_{1,2}a_{2,4}+\left(a_{1,3}+a_{1,2}a_{2,3}\right)\left(a_{2,4}a_{3,2}+a_{3,4}\right)\\ a_{4,1}+a_{2,1}a_{4,2}+\left(a_{3,1}+a_{2,1}a_{3,2}\right)\left(a_{2,3}a_{4,2}+a_{4,3}\right) & a_{4,4} \\ \end{array} \right)$
then apply
mi = Expand[Part[D, 1, 2]]
$a_{1,4}+a_{1,2} a_{2,4}+a_{1,3} a_{2,4} a_{3,2}+a_{1,2} a_{2,3}a_{2,4}a_{3,2}+a_{1,3} a_{3,4}+a_{1,2} a_{2,3} a_{3,4}$
Is there a way to write this code in the form of a program executed once without the need to repeat to make in general.
Such as matrix 5x5 we need redundancy 3 times to get 2x2 and the application of the condition