1

I am looking for the total distance from each point in a list to all points in a another. Specifically from each point in Asubopt to every point in A.

ClearAll["Global'*"];
n = 5
A = RandomVariate[NormalDistribution[5, 2], {n, 2}]
dtotal[f_] := Total[Sqrt[Total[(Transpose[f] - {x, y})^2]]]
minA = FindMinimum[dtotal[A], {x}, {y}]
Asub = Subsets[A, {1, n}]
Asubdist = Map[dtotal, Subsets[A, {1, n}]]
Asubopt = {x, y} /. FindMinimum[#, {x}, {y}][[2]] & /@ Asubdist
Table[Outer[EuclideanDistance, {Asubopt[[i]]}, A, 1], {i, 1, n}]`

I read

but the suggested Table does not seem to return the correct output.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 1
    There are a number of syntax errors in your code. The second line does not evaluated properly and the use of FindMinimum is incorrect as well. Please review your code for syntax errors first. – Sjoerd Smit Nov 06 '17 at 16:54
  • 1
    Try DistanceMatrix[Asubopt, A] – Carl Woll Nov 06 '17 at 16:54
  • Thanks I added the space in the second line and DistanceMatrix[Asubopt,A] does contain the information I need but also computes all the distances to points in Asubopt as well. – CharlesBouwer Nov 06 '17 at 17:00
  • I have used NMinimize as well.
    ClearAll["Global'*"]; n = 5 A = RandomVariate[NormalDistribution[5, 2], {n, 2}]
     dtotal[f_] := Total[Sqrt[Total[(Transpose[f] - {x, y})^2]]]
    
     minA = NMinimize[dtotal[A], {x, y}]
    
     Asub = Subsets[A, {1, n}]
    
     Asubdist = Map[dtotal, Subsets[A, {1, n}]]
    
     Asubopt = {x, y} /. NMinimize[#, {x, y}][[2]] & /@ Asubdist
    
    – CharlesBouwer Nov 06 '17 at 17:04
  • 1
    Thanks Carl Total[DistanceMatrix[Asubopt,A],{2}] was what I was looking for! – CharlesBouwer Nov 06 '17 at 17:39
  • This is not minimal distance, but total distance. I'll change the question head appropriately. – Alexey Popkov Nov 07 '17 at 06:52

1 Answers1

3

From the comments, the solution is

Total[DistanceMatrix[Asubopt,A],{2}]
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368