Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Kuba, rasher, Mr.Wizard Apr 16 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  
This and this are a duplicates, the only difference is that you have to include Sort for second column. –  Kuba Apr 16 at 7:59
    
Related: (16115), (16507), (26574). Loosely related: (17041) –  Mr.Wizard Apr 16 at 9:32

1 Answer 1

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[#]] &]]]
share|improve this answer

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