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.

I have followings set:

t = Transpose[{x[#] & /@ Range[20], RandomInteger[15, 20]}];
Subscript[#[[1]], #[[2]]] & /@ t

The code is just to build my set.So my set of variables is something like:

{Subscript[x[1], 7], Subscript[x[2], 15], Subscript[
x[3], 3], Subscript[x[4], 4], Subscript[x[5], 0], Subscript[
x[6], 1], Subscript[x[7], 15], Subscript[x[8], 1], Subscript[
x[9], 0], Subscript[x[10], 2], Subscript[x[11], 0], Subscript[
x[12], 10], Subscript[x[13], 12], Subscript[x[14], 11], Subscript[
x[15], 9], Subscript[x[16], 8], Subscript[x[17], 11], Subscript[
x[18], 4], Subscript[x[19], 0], Subscript[x[20], 1]}

How can I pick and add up all variables which has the same subscripts from my set.For example like:

Subscript[x[14], 11]+Subscript[x[17], 11]
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Try to use Gather like this:

t = Transpose[{x[#] & /@ Range[20], RandomInteger[15, 20]}];
d = Subscript[#[[1]], #[[2]]] & /@ t;
Total /@ Gather[d, #1[[2]] == #2[[2]] &]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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