0
$\begingroup$

I'm trying to simplify a table, for instance

{{1, {4, 2, 0}}, {4, {2, 4, 0}}, {-2, {3, 3, 0}}, {1, {3, 0, 3}}, {-3, {0, 4, 2}}}

Here the first column is the "weight" of the corresponding vector in the second column.

In the specific problem, the ordering of the entries in the vectors in the second column do not matter, so these can be sorted. I then want to gather the vectors, such that ones that are equivalent only appear once. In the process, I want to sum the weights. For instance, in the above example, the result would be

{{2, {4, 2, 0}}, {-1, {3, 3, 0}}

Any help would be greatly appreciated!

$\endgroup$

marked as duplicate by Kuba, ciao, Mr.Wizard Apr 16 '14 at 9:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ This and this are a duplicates, the only difference is that you have to include Sort for second column. $\endgroup$ – Kuba Apr 16 '14 at 7:59
  • $\begingroup$ Related: (16115), (16507), (26574). Loosely related: (17041) $\endgroup$ – Mr.Wizard Apr 16 '14 at 9:32
1
$\begingroup$
list = {{1, {4, 2, 0}}, {4, {2, 4, 0}}, {-2, {3, 3, 0}}, {1, {3, 0, 3}}, {-3, {0, 4, 2}}};
Transpose[{Plus @@@ #[[All, All, 1]], #[[All, 1, 2]]} &[GatherBy[list, Sort[Last[#]] &]]]
$\endgroup$

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