# How to eliminate rows and columns of matrices?

I would like to create an operation that checks if the elements of the row are 0 and eliminate the respective row and column associated with the row matrix. If if the lines have at least one non-zero element, then the matrix is copied as is. See figure below Suppose I have a list of matrices that increases in size, as for example

    list={{{0, 0}, {1, 0}}, {{1, 1, 1}, {0, 1, 0}, {1, 1, 1}}, {{0, 0, 0,
0}, {1, 0, 1, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}}, {{0, 1, 1, 1,
1}, {1, 0, 0, 1, 0}, {0, 1, 0, 1, 1}, {1, 1, 0, 0, 1}, {0, 1, 1, 1,
0}}, {{0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 1,
0}, {1, 1, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 1}, {0, 1, 0, 0, 1,
0}}, {{0, 1, 0, 1, 0, 0, 1}, {1, 0, 0, 0, 0, 1, 0}, {1, 0, 0, 0, 0,
0, 0}, {0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 1, 0, 0, 1}, {0, 0, 0, 1,
1, 0, 1}, {0, 0, 1, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, 0, 1, 1}, {1, 0,
0, 1, 1, 0, 1, 1}, {0, 1, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0,
0}, {0, 1, 0, 1, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 1,
0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}, {{0, 0, 1, 1, 0, 0, 0,
0, 0}, {1, 0, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 1, 0, 0,
0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 1, 0, 0, 1}, {0,
0, 0, 0, 0, 0, 1, 0, 0}, {1, 0, 0, 1, 1, 1, 0, 1, 0}, {0, 0, 0, 0,
0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}, {{0, 0, 1, 0, 1, 0,
0, 1, 0, 0}, {1, 0, 0, 1, 1, 0, 0, 0, 1, 1}, {1, 1, 0, 1, 0, 0, 0,
0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 1, 0, 1, 1, 0,
0, 1}, {0, 0, 1, 0, 1, 0, 1, 1, 0, 0}, {1, 1, 0, 1, 0, 0, 0, 1, 0,
1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0,
0}, {0, 1, 0, 0, 1, 1, 1, 0, 1, 0}}}


Could someone help me apply the rule to that list? Thanks in advance.

• Note that in your sample list all the elements of all the diagonals are 0 (check it with Diagonal /@ list), so your output would be empty for each matrix Jan 31, 2020 at 11:57
• @Fraccalo I edited the list for a case where there is no 0 in the main diagonal of the 3x3 matrix
– SAC
Jan 31, 2020 at 12:01

ClearAll[dropZRC1, dropZRC2, dropZRC3]
dropZRC1 = Module[{p = Flatten@Position[SparseArray[#]["MatrixColumns"], {__}]},
#[[p,p]]] &;

dropZRC2 = Module[{p = Flatten@Position[#, {___, Except[0], ___}]}, #[[p, p]]] &;

dropZRC3 = Module[{p = Position[#, {0 ..}]}, Nest[Transpose[Delete[#, p]] &, #, 2]] &;


For OP's list all three methods give the same results:

And @@ (dropZRC1[#] == dropZRC2[#] == dropZRC3[#] & /@ list)


True

Row[MatrixForm /@ list] // TeXForm


$$\tiny\begin{array}{ccc} \left( \begin{array}{cc} 0 & 0 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 1 & 0 \\ 1 & 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 1 & 0 & 1 & 1 \\ 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 \\ \end{array} \right) \\ \left( \begin{array}{ccccc} 0 & 1 & 1 & 1 & 1 \\ 1 & 0 & 0 & 1 & 0 \\ 0 &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 0 \\ 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccccccccc} 0 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 1 \\ 1 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 \\ 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 & 0 \\ \end{array} \right) \\ \end{array}$$

Row[MatrixForm /@ dropZRC1 /@ list] // TeXForm


$$\tiny\begin{array}{ccc} \left( \begin{array}{c} 0 \\ \end{array} \right) & \left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 1 & 0 \\ 1 & 1 & 1 \\ \end{array} \right) & \left( \begin{array}{ccc} 0 & 1 & 1 \\ 0 & 0 & 1 \\ 1 & 1 & 0 \\ \end{array} \right) \\ \left( \begin{array}{ccccc} 0 & 1 & 1 & 1 & 1 \\ 1 & 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 & 1 \\ 1 & 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cccccc} 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cccccc} 0 & 0 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 1 & 0 & 1 \\ 0 & 1 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cccccc} 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 1 & 1 \\ 1 & 1 & 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 1 & 1 & 0 \\ \end{array} \right) & \left( \begin{array}{ccccccc} 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 1 & 0 & 0 & 1 \\ 1 & 1 & 0 & 0 & 0 & 0 & 1 \\ 1 & 1 & 0 & 0 & 1 & 1 & 1 \\ 0 & 0 & 1 & 1 & 0 & 1 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 1 & 1 & 1 & 0 \\ \end{array} \right) \\ \end{array}$$

• Cool idea p = Position[#, {0 ..}]! Jan 31, 2020 at 15:10
• Nice use of SparseArray. But why "MatrixColumns" instead of "AdjacencyLists"? Jan 31, 2020 at 15:40
• @Mr.Wizard, that works too (totally forgot about it and went with the first property that gave what i needed:)
– kglr
Jan 31, 2020 at 15:43

I would do it this way: 1) find the position on the elements equal to $$1$$ on the diagonal, 2) select the corresponding rows and columns.

operate[mat_] := With[{pos = Flatten@Position[Diagonal@mat, 1]}, mat[[pos, pos]]]
operate@{{1, 0}, {1, 0}}
(* {{1}} *)
operate@{{1, 0, 1}, {1, 0, 1}, {0, 0, 1}}
(* {{1, 1}, {0, 1}} *)
operate@{{0, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0}, {1, 0, 1, 0}}
(* {{1, 0}, {1, 1}} *)
operate/@list
(* {{}, {{1, 1, 1}, {0, 1, 0}, {1, 1, 1}}, {}, {}, {}, {}, {}, {}, {}} *)


Edit The question has been changed (replacing "diagonal" with row) so of course this answer no longer makes sense...

A bit messy, but it should work:

process[m_] := Block[{t = m, temp, ind},
temp = Position[Diagonal[t], 0];
ind = temp - Range[0, Length@temp - 1];
((t = Transpose[Drop[Transpose[Drop[t, #]], #]]) & /@ ind)[[-1]]
]

process[{{1, 0}, {1, 0}}]


{{1}}

process[{{1, 0, 0, 0}, {1, 0, 1, 0}, {0, 1, 1, 0}, {1, 0, 0, 0}}]


{{1, 0}, {0, 1}}