# For loop over a list for pairwise distance implementation in Mathematica

I am new to Mathematica and want to implement the following functionality in Mathematica. Suppose I have the following list

data4 = {{1, 2}, {2, 3}, {1, 3}};


I want to calculate the Euclidean distance between consecutive elements and also maintain a value for the total of euclidean distances.A rough pseudocode in java would look like:

 for(i=0; i<data4.length; i++)
{
distance_array = data[i+1] - data[i];
total = total+distance;
}


I tried this in Mathematica.but it failed.

 knot2 = Table[EuclideanDistance[i, i + 1], {i, data4}];

-

## 4 Answers

This

Table[EuclideanDistance[i, i + 1], {i, data4}];


fails because for every step, i is a two-element list, like {1,2}. So i+1 is just {1,2}+1 or {2,3}. This works,

Table[EuclideanDistance[data4[[i]], data4[[i + 1]]], {i, Length@data4 - 1}]


This also works, and is a bit shorter,

Norm /@ (Rest@(data4 - RotateRight[data4]))
(* {Sqrt[2], 1} *)


Accumulate gives a running total, and Total gives the total distance,

Accumulate[Norm /@ (Rest@(data4 - RotateRight[data4]))]
Total[Norm /@ (Rest@(data4 - RotateRight[data4]))]
(* {Sqrt[2], 1 + Sqrt[2]} *)
(* 1 + Sqrt[2] *)


## Edit

I think this is what you are trying to do, you have your data

data4 = RandomInteger[20, {5, 2}]
(* {{19, 17}, {8, 7}, {1, 14}, {16, 16}, {2, 13}} *)


you have your distances,

distances = Table[EuclideanDistance[data4[[i]], data4[[i + 1]]], {i, Length@data4 - 1}]
(* {Sqrt[221], 7 Sqrt[2], Sqrt[229], Sqrt[205]} *)


And this is your normalized running total,

Accumulate[distances]/Total[distances]

(* {Sqrt[221]/(7 Sqrt[2] + Sqrt[205] + Sqrt[221] + Sqrt[229]), (
7 Sqrt[2] + Sqrt[221])/(
7 Sqrt[2] + Sqrt[205] + Sqrt[221] + Sqrt[229]), (
7 Sqrt[2] + Sqrt[221] + Sqrt[229])/(
7 Sqrt[2] + Sqrt[205] + Sqrt[221] + Sqrt[229]), 1} *)

-
Hi jason, but how to maintain a variable and add all the distances...do we need to loop over again? – kranthi kumar Mar 1 at 8:44
@kranthikumar - I guess I was unclear about what you mean there. If you just want the total of the distances, Total@Table[EuclideanDistance[data4[[i]], data4[[i + 1]]], {i, Length@data4 - 1}] will work. – JasonB Mar 1 at 8:45
Thank you !! I am trying to generate t0,t1,t2....tn as knots to pass them as a parameter to a function I developed. It should take the list we generated just now and calculate its values such that they are between 0,1 as below. t0=0 t1=|D1-D0|/Total t2 = (|D1-D0|+|D2-D0|)/Total ......tn knot1 = Table[t, {t, 0.0, 1.0, 1/m}]; more info if you want what i am trying to do is in link below cs.mtu.edu/~shene/COURSES/cs3621/NOTES/INT-APP/… Can you help on this too please.... – kranthi kumar Mar 1 at 9:24
@kranthikumar, in your expression in that comment, is Total, the running total (as in the total so far) or the total distance of the whole list? – JasonB Mar 1 at 9:40
its the Total distance of all the pairwise distances we calculated – kranthi kumar Mar 1 at 9:41

like this?

data4 = {{1, 2}, {2, 3}, {1, 3}};
Norm /@ Differences[data4]

{Sqrt[2], 1}


total of Euclidean distances

Total[Norm /@ Differences[data4]]

1 + Sqrt[2]

-

Just some variants:

{##, Total@##} &@(EuclideanDistance @@@ Partition[data4, 2, 1])
{##, Total@##} &@(Sqrt[#.#] & /@ Differences[data4])


both yield: {{Sqrt[2], 1}, 1 + Sqrt[2]}

-
f = DeveloperPartitionMap[Sqrt[Plus @@ ((Subtract @@ #)^2)] &, #, 2, 1 ] &;
(* or f = DeveloperPartitionMap[EuclideanDistance @@ # &, #, 2, 1 ] &; *)
f@{{19, 17}, {8, 7}, {1, 14}, {16, 16}}


{Sqrt[221], 7 Sqrt[2], Sqrt[229]}

Accumulate@%


{Sqrt[221], 7 Sqrt[2] + Sqrt[221], 7 Sqrt[2] + Sqrt[221] + Sqrt[229]}

-