I am still learning to use Mathematica and I need some help. How could I get the most common pair, and generally the most common n-tuple of a list (row vector) given a list of lists of m x 5
dimensions? Suppose as a sample I have a list of lists, dimension 9 x 5
of numbers
A =
53 21 2 49 3
44 31 26 35 46
3 45 44 24 19
19 25 60 12 16
31 44 20 35 46
21 53 2 49 20
5 23 49 18 27
7 22 33 42 58
21 53 2 49 5
More specifically in Mathematica code:
A = {
{53, 21, 2, 49, 3}, {44, 31, 26, 35, 46}, {3, 45, 44, 24, 19},
{19,25, 60, 12, 16}, {31, 44, 20, 35,46}, {21, 53, 2, 49, 20},
{5, 23, 49, 18, 27}, {7, 22, 33, 42, 58}, {21, 53, 2, 49, 5}}
Suppose I want to find the most common 4-tuple of the entire list of lists (A
). One scenario could be {53, 21, 2, 49}
is the most common 4-tuple of the list of lists. Another scenario could be that {44, 31, 35, 46}
is the most common 4-tuple.
More concretely, in the given list A
, the most common 4-tuple is {53, 21, 2, 49}
since that 4-tuple occurs three times-- one on the first row, one the sixth row, and one on the last row. As you may have noticed, the order does not matter.
The second most common 4-tuple would be {44, 31, 35, 46}
since it occurs twice--once on the second row and again on the fifth row.
And then calling on Tally
to get a result like {{{53, 21, 2, 49},3}, {{44, 31, 35, 46}, 2},...}
I would like to be able to do this with 2-tuples, 3-tuples, 4-tuples, and 5-tuples. I was able to do 1-tuples trivially by using Flatten[A]
on the list of lists then Tally[%]
. Any guidance, tips, suggestions is appreciated.
Tally[Flatten[Subsets[Sort[#], {4}] & /@ A, 1]]
and report back. $\endgroup$Subsets[]
, BTW. $\endgroup$