0
$\begingroup$

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]} 
$\endgroup$
2
  • $\begingroup$ The second argument of Apply matches mr. Why not just stick that in? You are also confusing the use cases of Map (short form - /@) and Apply. meanMR = Mean /@ mr returns {1.64748, 1.13594}. $\endgroup$
    – LLlAMnYP
    Sep 22, 2015 at 10:26
  • $\begingroup$ i swear i tried that! Thank you. $\endgroup$ Sep 22, 2015 at 10:30

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thank you for taking the time to answer in detail. That was very helpful! $\endgroup$ Sep 22, 2015 at 16:27

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