Search code examples
wolfram-mathematicaeuclidean-distance

Apply EuclideanDistance at a certain level in Mathematica


Please consider:

daList = {{{21, 18}, {20, 18}, {18, 17}, {20, 15}}, 
          {{21, 18}, {20, 18}, {21, 14}, {21, 14}}};

I would like to compute the distance between each point in the 2 sub-lists of that list:

Yet I need to use a Function to apply at the correct level:

Function[seqNo, 
         EuclideanDistance[#, {0, 0}] & /@ daList[[seqNo]]] /@ 
         Range[Length@daList]

out = {{3 Sqrt[85], 2 Sqrt[181], Sqrt[613], 25}, {3 Sqrt[85], 2 Sqrt[181], 
        7 Sqrt[13], 7 Sqrt[13]}}

Is there a way to avoid this heavy function there? To specify the level avoiding my Function with seqNo as argument? :

EuclideanDistance[#, {0, 0}] & /@ daList

out={EuclideanDistance[{{21, 18}, {20, 18}, {18, 17}, {20, 15}}, {0, 0}], 
     EuclideanDistance[{{21, 18}, {20, 18}, {21, 14}, {21, 14}}, {0, 0}]}

Solution

  • Have you tried the Level specification in Map?

    Map[EuclideanDistance[#, {0, 0}] &, daList, {2}]
    

    gives

    {{3 Sqrt[85],2 Sqrt[181],Sqrt[613],25},{3 Sqrt[85],2 Sqrt[181],7 Sqrt[13],7 Sqrt[13]}}