1

I am looking for the distance between the spatial median of a subset of points to all original points in the set preferences to model group decision making. I can build a for loop to return the subsets spatial medians but am having trouble extracting the optimized distances. Any help is greatly appreciated.

n = 5
preferences = RandomVariate[NormalDistribution[5, 2], {n, 2}]
dtotal[f_] := Total[Sqrt[Total[(Transpose[f] - {x, y})^2]]]
sodtotal = FindMinimum[dtotal[preferences], {x}, {y}]
sopt = {x /. sodtotal[[2]], y /. sodtotal[[2]]}
sodist = sodtotal[[1]]
SpatialMedian[preferences]
prefsub = Subsets[preferences, {1, n}]
subdtotal = Map[dtotal, Subsets[preferences, {1, n}]]
subopt = {}
For[i = 1, i <= Length[subdtotal], i++, 
 subopt = Append[
   subopt, {x /. FindMinimum[subdtotal[[i]], {x}, {y}][[2]], 
    y /. FindMinimum[subdtotal[[i]], {x}, {y}][[2]]}]]
subopt

Creating Lists From Loops How do I find the Euclidean distance between one point and all the points in a list?

1 Answers1

6

You can replace your subopt initialization and the For loop with:

subopt = {x, y} /. FindMinimum[#, {x}, {y}][[2]] & /@ subdtotal

Or using Map explicitly

{x, y} /. Map[FindMinimum[#, {x}, {y}][[2]] &, subdtotal]
qbit
  • 253
  • 1
  • 4
  • Thank you that Map function replaces the loop very well. I was hoping to use the Nminimize function for the distance from each point in subopt to all points in preferences. – CharlesBouwer Nov 02 '17 at 21:25