1
$\begingroup$

Arrange the table cells according to the number of elements in the cell and add a column containing the number of elements in each cell.

If I have the lists

S1 = {Subscript[x, 1] Subscript[x, 2] , Subscript[x, 3] };
S2 = {Subscript[x, 4] Subscript[x, 5], 
   Subscript[x, 6] Subscript[x, 7] Subscript[x, 8]};

I use this code to create a table

TableForm[
 ToExpression /@ (StringRiffle[#, {"{", "", "}"}] & /@ 
    Tuples[{S1, S2}]), TableHeadings -> Automatic]

The output is in the form:

enter image description here

I need to sort the table cells according to the number of items in the cell in ascending order.

enter image description here

Then add a column containing the number of elements in each cell. The output will be in the form

enter image description here

Thanks for the help.

$\endgroup$

1 Answer 1

1
$\begingroup$

Convert each tuple to a simple list so the elements can be counted. Make a table of the lists multiplied together, with the length of each list. Sort the table by length.

S1 = {Subscript[x, 1] Subscript[x, 2] , Subscript[x, 3]};
S2 = {Subscript[x, 4] Subscript[x, 5], 
   Subscript[x, 6] Subscript[x, 7] Subscript[x, 8]};

TableForm[
  SortBy[
    Table[{Times[Sequence@@t], Length@t},
      {t, Flatten/@Tuples[{S1, S2} /. Times -> List]}],
    Last],
  TableHeadings -> {Automatic, {"cut element", "order"}}]

ordered table

Here's a simpler method that uses Variables to make lists from the tuples.

TableForm[
  SortBy[
    Table[{Times[Sequence@@t], Length@t},
     {t, Variables/@Tuples[{S1, S2}]}],
    Last],
  TableHeadings -> {Automatic, {"cut element", "order"}}]
$\endgroup$
0

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.