# Selecting matrices based on existence of unique column and row of all equal terms

So, I have a list of lists of lists such as the following:

{{{4}},
{{7, 7}, {7, 7}},
{{5, 5}, {6, 6}},
{{2, 1}, {1, 1}}}


Interpreting the elements of this list as $$n\times n$$ matrices, I want to iterate through and select matrices where exactly one column and one row consist only of a particular element which only occurs in that column or row.

So, building from the example above, {{4}} would be selected as there is only one column or row to consider. {{7, 7}, {7, 7}} would not be selected as, while there is a column and row that each contain only 7, there is also a 7 not contained in that column or row.{{5, 5}, {6, 6}} would not be selected because, while there are rows that fit our requirement, there are no columns. {{2, 1}, {1, 1}} would be selected as it satisfies all requirements.

What are some ways that you could put together a function to identify these matrices?

• "Interpreting the elements of this list as n x n matrices" Well, you can't. Three of the elements are 2 x 2 matices but the 1st isn't a 2 x 2 matrix or an n x n matrix for any other n, so the whole idea collapses. May 10, 2019 at 4:19
• Ah, that's just a 1x1 matrix. Arguably there are no meaningful difference between a 1x1 matrix and a single element, but that doesn't mean you can't still consider it a matrix. May 10, 2019 at 12:10

ClearAll[f1, f2, f3]
f1 = Cases[{___, {Except[a_]..., a_, Except[a_]...}, ___}]@*Cases[{___, {(a_)..}, ___}];
f2 = Select[MemberQ[Tally /@ #, {_, 1}, All] &]@*Cases[{___, {(a_)..}, ___}] ;
f3 = Pick[#, MemberQ[#, {_, 1}, All]& /@ Map[Tally, #, {2}]]& @*Cases[{___, {(a_)..}, ___}];


Examples:

lst1 = {{{4}}, {{7, 7}, {7, 7}}, {{5, 5}, {6, 6}}, {{2, 1}, {1, 1}}};
f1 @ lst1


{{{4}}, {{2, 1}, {1, 1}}}

f1 @ lst1 == f2 @ lst1 == f3 @lst1


True

lst2 = {{{3, 3}, {3, 2}}, {{1, 1, 1, 1}, {1, 2, 3, 0}, {1, 0, 0,
0}, {1, 0, 3, 2}}, {{1, 1, 1, 1, 1}, {1, 4, 3, 0, 4}, {1, 3, 3,
3, 0}, {1, 0, 3, 4, 4}, {1, 4, 0, 4, 4}}} ;
f1 @ lst2 == f2 @lst2 == f3 @ lst2 == lst2


True

• Thank you for the different versions. This gives me a lot of stuff to read up on. May 10, 2019 at 12:11
list = {{{4}}, {{7, 7}, {7, 7}}, {{5, 5}, {6, 6}}, {{2, 1}, {1, 1}}}

Select[list,Length[s=Select[#,Length@Union@#==1&]]==1&&Equal[s&/@{#,Transpose@#}]&@#&]


{{{4}}, {{2, 1}, {1, 1}}}

• Running this returns only 1x1 or 2x2 matrices. What if the list of matrices contain larger matrices, such as 4x4 or 5x5? May 10, 2019 at 1:47
• @Rithaniel Did you try it with larger matrices and it didn't work? May 10, 2019 at 1:52
• Yes, with: {{{3, 3}, {3, 2}}, {{1, 1, 1, 1}, {1, 2, 3, 0}, {1, 0, 0, 0}, {1, 0, 3, 2}}, {{1, 1, 1, 1, 1}, {1, 4, 3, 0, 4}, {1, 3, 3, 3, 0}, {1, 0, 3, 4, 4}, {1, 4, 0, 4, 4}}} it returned: {{{3, 3}, {3, 2}}} May 10, 2019 at 1:56
• I believe that now it works May 10, 2019 at 2:22
• Indeed it does. Thank you for the help. May 10, 2019 at 12:12