7
$\begingroup$

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 enter image description here 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.

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Fraccalo
    Commented Jan 31, 2020 at 11:57
  • $\begingroup$ @Fraccalo I edited the list for a case where there is no 0 in the main diagonal of the 3x3 matrix $\endgroup$
    – SAC
    Commented Jan 31, 2020 at 12:01

3 Answers 3

8
$\begingroup$
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 & 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}{ccccccc} 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 \\ \end{array} \right) \\ \left( \begin{array}{cccccccc} 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 \\ \end{array} \right) & \left( \begin{array}{ccccccccc} 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 \\ \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}$

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

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...

$\endgroup$
1
$\begingroup$

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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.