1
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ "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. $\endgroup$
    – m_goldberg
    Commented May 10, 2019 at 4:19
  • $\begingroup$ 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. $\endgroup$
    – Rithaniel
    Commented May 10, 2019 at 12:10

2 Answers 2

1
$\begingroup$
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

$\endgroup$
1
  • $\begingroup$ Thank you for the different versions. This gives me a lot of stuff to read up on. $\endgroup$
    – Rithaniel
    Commented May 10, 2019 at 12:11
1
$\begingroup$
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}}}

$\endgroup$
7
  • $\begingroup$ Running this returns only 1x1 or 2x2 matrices. What if the list of matrices contain larger matrices, such as 4x4 or 5x5? $\endgroup$
    – Rithaniel
    Commented May 10, 2019 at 1:47
  • $\begingroup$ @Rithaniel Did you try it with larger matrices and it didn't work? $\endgroup$
    – ZaMoC
    Commented May 10, 2019 at 1:52
  • $\begingroup$ 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}}} $\endgroup$
    – Rithaniel
    Commented May 10, 2019 at 1:56
  • 1
    $\begingroup$ I believe that now it works $\endgroup$
    – ZaMoC
    Commented May 10, 2019 at 2:22
  • 1
    $\begingroup$ Indeed it does. Thank you for the help. $\endgroup$
    – Rithaniel
    Commented May 10, 2019 at 12:12

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.