# Calculating average distance

I have a program that gives the average distance as the output. When I tried to repeat finding the average distance 100 times using Table, It failed to generate the output. This is the program.

Xarray = A @@@ Tuples[Range[0, 4], 3];
Table[M = RandomSample[Xarray, 7];
energies = RandomVariate[ExponentialDistribution[1.5], {7}];
f = {#, First@Pick[energies, M, #]} &;
list = Map[f, M]
c = Subsets[Range[Length@list], {2}];
d = Length@%

distanceBetween[{n_, m_}] :=
Norm[List @@ list[[n, 1]] - List @@ list[[m, 1]]]

Map[distanceBetween, c]
ans = % // N;

Total[%/d], {i, 100}]


The program is like this. I created an array of let say 500 elements and assigned energy to it. Next I choose a coordinate from the list and find the distance from other coordinates using the norm equation. Next I sum up that distances in the list and take the average of the distance. The Problem comes when I am trying to do this process 100 times using Table or Do command.

-
Could you post the Table command you used. You should not use symbols that start with a capital letter such as Xarray or A, M as those might shadow MMa symbols. – Matariki Jul 14 '12 at 10:21
I had posted it now. – Daniel Jul 14 '12 at 10:27
A note, Length@Xarray; doesn't seem to do anything, is there a use for it ? And what is the purpose of A, it seems redundant? – image_doctor Jul 14 '12 at 10:28
Thanks, but your Table command is incomplete. Also you are assigning repeatedly a list to a single variable. do you mean m = Table[RandomSample[Xarray,7],{100}] or so? – Matariki Jul 14 '12 at 10:30
I didn't edited the program. It helps me to know the number of elements in the array. – Daniel Jul 14 '12 at 10:32

Try this. It is not beautiful but ...

xarray = A @@@ Tuples[Range[0, 4], 3];

distanceBetween[{n_, m_}, list_] :=
Norm[List @@ list[[n, 1]] - List @@ list[[m, 1]]];

meanDist := Module[{m, c, f, list, energies},
m = RandomSample[xarray, 7];
energies = RandomVariate[ExponentialDistribution[1.5], {Length@m}];
f = {#, First@Pick[energies, m, #]} &;
list = Map[f, m];
c = Subsets[Range[Length@list], {2}];
Mean@Map[distanceBetween[#, list] &, c] // N]

Table[meanDist, {100}]


I hope this helps.

-
Thanks Matariki.. – Daniel Jul 14 '12 at 11:28

I see that you are at least using the answers to your previous questions to improve your code, but I do think you need to spend a bit more time with the documentation so that you better understand what you are doing. It really doesn't make people keen to help when you come back with the same problem and ask for help at every step.

Once you clean up the code with semicolons, you don't get errors any more, but the result is something like a 100-length vector where each element looks like:

 1/125 A[0, 0, 0] + 1/125 A[0, 0, 1] + 1/125 A[0, 0, 2] +
1/125 A[0, 0, 3] + 1/125 A[0, 0, 4] + 1/125 A[0, 1, 0] +
1/125 A[0, 1, 1] + 1/125 A[0, 1, 2] + 1/125 A[0, 1, 3] +
1/125 A[0, 1, 4] + 1/125 A[0, 2, 0] + <<103>> + 1/125 A[4, 2, 4] +
1/125 A[4, 3, 0] + 1/125 A[4, 3, 1] + 1/125 A[4, 3, 2] +
1/125 A[4, 3, 3] + 1/125 A[4, 3, 4] + 1/125 A[4, 4, 0] +
1/125 A[4, 4, 1] + 1/125 A[4, 4, 2] + 1/125 A[4, 4, 3] +
1/125 A[4, 4, 4]


I'm sure that's not what you intended. The use of % inside the Table (or Module) seems unnecessary and I think it is part of the problem.

If you compress the last three lines inside your Table to

Total[N[Map[distanceBetween, c]] ]/d


you will get numerical data of the kind I think you are looking for.

There are some other problems with your code, e.g. the unnecessary manual calculation of mean instead of using Mean (so you can remove the other %). I also think that calculating the energies inside the Table means that if you select the same element in more than one of your RandomSamples, it will have a different energy each time. I am sure this is not what you intend. The problem here is not with your understanding of Mathematica, it is with your understanding of what you are trying to do. Try writing out step by step in pseudocode or math first, to test if you have made a conceptual error in your implementation.

I'll also point out that if you hadn't given the elements of Xarray a Head of A using A @@@, you wouldn't need the List @@ code further down in the distance between function.

In any case if you redefine your distanceBetween function like this, you won't need to keep inefficiently redefining it in each iteration of the Table.

distanceBetween[lst_, {n_, m_}] := Norm[List @@ lst[[n, 1]] - List @@ lst[[m, 1]]]


Alternatively, leave the elements of Xarray as lists not A[1,2,3] and so on, and you can just use the EuclideanDistance function instead of recoding it yourself.

-