1
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 6, 2017 at 16:54
  • 1
    $\begingroup$ Try DistanceMatrix[Asubopt, A] $\endgroup$
    – Carl Woll
    Nov 6, 2017 at 16:54
  • $\begingroup$ 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. $\endgroup$ Nov 6, 2017 at 17:00
  • $\begingroup$ 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 $\endgroup$ Nov 6, 2017 at 17:04
  • 1
    $\begingroup$ Thanks Carl Total[DistanceMatrix[Asubopt,A],{2}] was what I was looking for! $\endgroup$ Nov 6, 2017 at 17:39

1 Answer 1

3
$\begingroup$

From the comments, the solution is

Total[DistanceMatrix[Asubopt,A],{2}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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