# Sum up different arrays into a new array

I have a question regarding sums in arrays.

So I have the following array:

list=RandomReal[{0,1},{5,2}]

(*{{0.693551,0.447185},{0.274842,0.637526},{0.745271,0.0288363},{0.894933,0.937219},{0.605447,0.0337067}}*)


And from that I want to have the splitting for each pair like that

Subsets[Range@Length@list, {2}]
{{1,2},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5},{3,4},{3,5},{4,5}}


Lets say every array in the list is a pair of x and y coordinates. Now I compute the distance between the different points using:

dist = EuclideanDistance @@@ Subsets[list, {2}] .


But now I want to have the sum of all the distances where particle 1 is occuring, so these are the first 5 entries in the array, for particle 2 its the first and the 5, 6 and 7th etc.

In the end I want to have a list containing 5 arrays with the sum for each particle.

So can anyone help me please?

You can use DistanceMatrix and use Total with the upper part of the matrix:

rowtotaldistances = Total[UpperTriangularize[DistanceMatrix[#],1],{2}]&

m = Partition[Range[0,10],2];

TeXForm @ MatrixForm @ m


$$\left( \begin{array}{cc} 0 & 1 \\ 2 & 3 \\ 4 & 5 \\ 6 & 7 \\ 8 & 9 \\ \end{array} \right)$$

 TeXForm @ MatrixForm[UpperTriangularize[DistanceMatrix[m],1]]


$$\left( \begin{array}{ccccc} 0 & 2 \sqrt{2} & 4 \sqrt{2} & 6 \sqrt{2} & 8 \sqrt{2} \\ 0 & 0 & 2 \sqrt{2} & 4 \sqrt{2} & 6 \sqrt{2} \\ 0 & 0 & 0 & 2 \sqrt{2} & 4 \sqrt{2} \\ 0 & 0 & 0 & 0 & 2 \sqrt{2} \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

rowtotaldistances @ m // TeXForm


$$\left\{20 \sqrt{2},12 \sqrt{2},6 \sqrt{2},2 \sqrt{2},0\right\}$$

Note: Using {2} as the second argument of Total gives the row totals:

mat = Array[Subscript[a, ##] &, {5, 5}];
TeXForm@MatrixForm[mat]


$$\left( \begin{array}{ccccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} & a_{1,5} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} & a_{4,5} \\ a_{5,1} & a_{5,2} & a_{5,3} & a_{5,4} & a_{5,5} \\ \end{array} \right)$$

TeXForm @ MatrixForm[Total[m, {2}]]


$$\left( \begin{array}{c} a_{1,1}+a_{1,2}+a_{1,3}+a_{1,4}+a_{1,5} \\ a_{2,1}+a_{2,2}+a_{2,3}+a_{2,4}+a_{2,5} \\ a_{3,1}+a_{3,2}+a_{3,3}+a_{3,4}+a_{3,5} \\ a_{4,1}+a_{4,2}+a_{4,3}+a_{4,4}+a_{4,5} \\ a_{5,1}+a_{5,2}+a_{5,3}+a_{5,4}+a_{5,5} \\ \end{array} \right)$$

For column totals leave the second argument out (or use 1 as the second argument):

TeXForm @ MatrixForm[Total[mat]]


$$\left( \begin{array}{c} a_{1,1}+a_{2,1}+a_{3,1}+a_{4,1}+a_{5,1} \\ a_{1,2}+a_{2,2}+a_{3,2}+a_{4,2}+a_{5,2} \\ a_{1,3}+a_{2,3}+a_{3,3}+a_{4,3}+a_{5,3} \\ a_{1,4}+a_{2,4}+a_{3,4}+a_{4,4}+a_{5,4} \\ a_{1,5}+a_{2,5}+a_{3,5}+a_{4,5}+a_{5,5} \\ \end{array} \right)$$

• Okay, thanks but if I do that now with rowtotaldistances = Total[Most @ UpperTriangularize[DistanceMatrix[list],1],{2}] I just get 4 entries, but I have 5 particles – Armani42 Jun 21 '19 at 13:07
• @Armani42, removed Most to get a 5X5 distance matrix. – kglr Jun 21 '19 at 13:11

Thanks, but now I have another problem, now I have

ener[r2_] = 4* (1/r2^12 - 1/r2^6);


And I want to make now

dist = DistanceMatrix[list, DistanceFunction->(#1-#2 &)];


Which produces me 2x2 vectors in a matrix. But now I want to do that

e = Apply[ener @ {##}&, dist, {2}];


But here is the problem, that some entries in the vectors are 0 and dividing by zero is not possible.

So how can I avoid that maybe by an if statement or something like that?

• @kglr do you have an idea for that problem? :) – Armani42 Jun 21 '19 at 13:48