# How to map a function to a specfic level spec

I'm rewriting a function into a manipulatable graphic and while I'm happily surprised with how concise functional programming can be, I'm having trouble mapping the anonymous function Norm[#[[2]]-#[[1]]] & on to the pairs of points represented below:

Generate the data

timeSeries =
MapAt[RandomChoice[{
{#[[1]],#[[2]]+RandomVariate[NormalDistribution[0,sigma]]},
{#[[1]]+RandomVariate[NormalDistribution[0,sigma]],#[[2]]}
}] &,#,{#} &/@ RandomInteger[{1,Length@list},Ceiling[Length@list * (1-precision)]]] &,
list,20],{sigma,{5,10,15}}];


Output:

{Length@timeSeries,Length /@ timeSeries,Length /@ timeSeries[[1]], Length /@timeSeries[[1,1]],timeSeries[[1,1,1]]}

{3,{21,21,21},{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30},{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},{{114686.,132379.},{114686.,132379.}}}

Norm[#[[2]]-#[[1]]] & /@ timeSeries[[1,9]]

{2.43774,0.,8.73786,0.,0.,0.,0.,0.,0.446609,3.23359,3.73703,0.,8.29204,5.33826,0.,0.,0.,6.65264,3.52166,3.80975,8.55396,1.92847,0.,0.,0.,0.,0.,0.,0.,0.}


I had assumed this was a relatively common type of preprocessing for data sets but couldn't find a solution online. I've tried several configurations of Map, MapThread, etc. on different levelspec but haven't stumbled upon a combination that works yet. Moving past trial-and-error does anyone have any suggestions for how to properly map the above stated function on to all the pairs of 2D points?

Update: Added more info. list is the initial list of 2D points.

• What is list? What is precision? Please provide full data expamples. Moreover, please provide minimal examples. Norm does not appear in your input code for timeSeries, so why should we parse it? Aug 19, 2018 at 18:48
• Maybe Map[ Norm[#[[2]] - #[[1]]] &, timeSeries[[All, 9]], {2} ]? Aug 19, 2018 at 19:40

It's helpful to include a computable version of your question. I will fill in the missing items as follows:

list = RandomReal[10, {30, 2}];
precision = .1;

SeedRandom[1]
timeSeries = Table[
MapAt[
RandomChoice[{
{#[[1]],#[[2]]+RandomVariate[NormalDistribution[0,sigma]]},
{#[[1]]+RandomVariate[NormalDistribution[0,sigma]],#[[2]]}
}]&,
#,
RandomInteger[{1, Length@list},{Ceiling[Length@list * (1-precision)], 1}]
]&,
list,
20
],
{sigma, {5,10,15}}
];


(I also used RandomInteger[range, {len, 1}] instead of {#}& /@ RandomInteger[range, len]).

Now, you can get the norms you want with:

r1 = Apply[Norm @* Subtract, timeSeries, {3}];


or the equivalent:

r2 = Apply[EuclideanDistance, timeSeries, {3}];

r1 === r2


True

Compare this with your short example:

Norm[#[[2]]-#[[1]]]& /@ timeSeries[[1, 9]]
r1[[1, 9]]


{26.0051, 11.037, 8.0916, 8.23104, 7.98833, 12.2301, 11.7063, 9.0764, 5.33768, 4.82262, 12.9593, 21.5366, 4.0383, 14.8998, 4.38089, 14.8319, 12.7268, 9.46539, 21.2087, 12.4623, 10.3275, 13.3517, 8.42573, 1.181, 11.5442, 10.7561, 5.60134, 8.46001, 21.9047, 15.4427}

{26.0051, 11.037, 8.0916, 8.23104, 7.98833, 12.2301, 11.7063, 9.0764, 5.33768, 4.82262, 12.9593, 21.5366, 4.0383, 14.8998, 4.38089, 14.8319, 12.7268, 9.46539, 21.2087, 12.4623, 10.3275, 13.3517, 8.42573, 1.181, 11.5442, 10.7561, 5.60134, 8.46001, 21.9047, 15.4427}

• Working through your suggestion I finished with the expression of ListPlot[Map[Mean, Apply[EuclideanDistance, timeSeries, {3}], {2}] which I thought showed the sublte difference between Map & Apply really well in this case. Sometimes I'm not too sure how much information to include in the initial OP because I want to make sure I'm being considerate of other member's time. Thanks for the feedback. Aug 20, 2018 at 0:45