0

I have am trying to obtain the average of the distance from the mean radius for each shot in a group of shots at a target. I am having problems with the very last line. The output of the last line appears that the Apply statement is being applied to the correct level but it is not resolving.

code

ShotsPerGroup = 3  
NumberGroups = 2  
(* Define the group of shots as a random variable with a normal distribution *)  
RandomSeed[66936, Method -> All]  
R := RandomVariate[NormalDistribution[]];  
a = Flatten[
   Table[List[i, R], {i, 1, NumberGroups}, {j, 1, ShotsPerGroup}], 
   1];  
Print["a = ", TableForm[a]]  
(* Group the shots by group *)  
byGroup = GatherBy[a, First][[All, All, 2]];  
Print["byGroup =", byGroup]  
(* Calculate the distance from the mean radius of each shots by group *)  
mr = Sqrt[(#^2 + Mean[#]^2)] & /@ byGroup  
Print["mr = ", TableForm[mr]]  
(* obtain the mean of the group mean radius *)  
meanMR = 
 Apply[Mean, Sqrt[(#^2 + Mean[#]^2)] & /@ byGroup, 1]  

output from last line:

{Mean[1.1711, 0.717469, 0.980774], Mean[0.631837, 1.55101, 0.573568]} 
LLlAMnYP
  • 11,486
  • 26
  • 65

1 Answers1

1

Somewhat more detailed answer, though this question "arises due to a simple mistake..."

Mean takes a list as its argument. What you had was {{a, b, c}, {d, e, f}}. The FullForm of this is:

List[List[a, b, c], List[d, e, f]]

Apply at level 1 replaces the heads of list at level 1 like so:

List[Mean[a, b, c], Mean[d, e, f]]

while what you needed, was:

List[Mean[List[a, b, c]], Mean[List[d, e, f]]]

which is exactly the result of Mapping.

LLlAMnYP
  • 11,486
  • 26
  • 65