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$
2
  • 1
    $\begingroup$ This and this are a duplicates, the only difference is that you have to include Sort for second column. $\endgroup$ Commented Apr 16, 2014 at 7:59
  • $\begingroup$ Related: (16115), (16507), (26574). Loosely related: (17041) $\endgroup$ Commented Apr 16, 2014 at 9:32

1 Answer 1

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.