6

I am trying to model what is called the extreme spread of a series of shots at a target. Each shot is from a normal distribution with mean 0 and variance 1. For this example there are three shots per group and I have four groups to analyze.

The computation of the extreme spread is simply the maximum Euclidean distance between the shots, so I need to compute all of the Euclidean distances between the three shots per group. Then find the maximum distance for each group.

The following code attempts to do that except the last step (the do loop) doesn't work. I'm new to Mathematica, so I'm not even sure my syntax is correct - except that Mathematica doesn't return an error.

Clear["Global`*"]
μ = 0
σ = 1
RandomSeed[66936, Method -> All];
\[ScriptCapitalD] = NormalDistribution[μ, σ];
rv := RandomVariate[\[ScriptCapitalD]];
shotsPerGroup = 3
iterations = 4
data = Flatten[Table[List[i, rv], {i, 1, iterations}, {j, 1, shotsPerGroup}], 1];
groupBy = GatherBy[data, First][[All, All, 2]];

edist = {}
Do[
  Append[edist, EuclideanDistance[groupBy[[i, j]], groupBy[[i, k]]]],
  {i, 1, iterations},
  {j, i, shotsPerGroup - 1},
  {k, j + 1, shotsPerGroup}
]
edist
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453

2 Answers2

11

More Mathematica-ish

SeedRandom[42];
mu = 0;
sigma = 1;
shotsPerGroup = 3;
iterations = 4;
rv = RandomVariate[NormalDistribution[mu, sigma], {iterations, shotsPerGroup, 2}];
pairs = Subsets[#, {2}] & /@ rv;
Max /@ Apply[EuclideanDistance, pairs, {2}]
(*
 {1.29553, 3.05122, 1.24002, 2.10169}
*)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
7

Your code is almost completely correct, but I did notice your use of Append which actually doesn't reassign the value of edist, for that, you would to use AppendTo.

enano9314
  • 543
  • 4
  • 9