3
$\begingroup$

I have the following list of points (ccmppp1) as an output from part of my scripts which is a list of lists. To find the Mean of each sublist, I am using the following script which works well.

trcentrpp = Mean /@ ccmppp1

However, When my output is a list (e.g ccmppp2) the above script gives the wrong output and I should use the following script.

trcentrpp = Mean @ ccmppp2

Considering that I do not know if my output is a list or list of list which command should I use that works for both. Or should I use the If function which I prefer not to use?

ccmppp1 = {{{45.8144, -0.864685, 10.7009}, {42.6579, -0.863343, 10.01}, {44.2365, -0.830501, 10.3459}, {171.179, 305.888, -107.992}}, {{173.75, 306.062, -108.301}, {171.775, 307.219, -110.29}, {174.06, 307.508, -111.119}, {170.034, 307.95, -111.296}, {174.329, 309.043, -113.554}, {172.091, 309.352, -113.408}, {169.571, 309.361, -113.06}, {169.98, 311.135, -115.16}, {174.687, 311.218, -116.291}, {172.25, 311.487, -115.947}, {173.124, 312.642, -117.252}, {174.887, 312.839, -117.922}}, {{168.792, 313.106, -116.923}, {171.072, 313.217, -117.375}, {173.008, 314.374, -118.78}, {175.094, 314.559, -119.475}, {169.427, 314.727, -118.452}, {171.107, 314.919, -118.858}, {173.699, 315.855, -120.169}, {170.027, 316.188, -119.749}, {175.261, 316.204, -120.815}, {171.812, 316.454, -120.228}, {173.568, 317.473, -121.382}, {169.795, 317.828, -121.02}, {175.483, 318.03, -122.247}, {171.704, 318.423, -121.752}}}

ccmppp2 = {{171.812, 316.454, -120.228}, {173.568, 317.473, -121.382}, {169.795, 317.828, -121.02}, {175.483, 318.03, -122.247}, {171.704, 318.423, -121.752}, {173.455, 319.502, -122.871}, {175.579, 319.568, -123.379}, {171.569, 320.011, -122.921}, {172.051, 321.326, -123.958}, {173.959, 321.362, -124.319}, {175.775, 321.389, -124.718}, {172.506, 323.092, -125.281}, {174.385, 323.193, -125.675}, {176.134, 323.528, -126.258}, {174.903, 324.765, -126.81}, {173.108, 325.129, -126.751}, {176.3, 325.268, -127.376}, {174.762, 326.297, -127.72}, {176.462, 327.022, -128.426}, {176.555, 328.653, -129.344}, {178.005, 342.933, -137.562}, {178.125, 344.653, -138.456}, {178.235, 346.388, -139.252}, {75.5718, 436.359, -80.7434}, {76.131, 438.132, -87.7129}, {76.9078, 439.652, -94.7067}}

$\endgroup$
6
$\begingroup$

Try Map[Mean, #, {-3}]

Map[Mean, ccmppp1, {-3}]
{{75.9719, 75.8324, -19.2338}, {172.545, 309.651, -113.633}, 
 {172.132,315.811, -119.802}}
Map[Mean, ccmppp2, {-3}]
{163.186, 337.94, -121.956}

This would also work if you have even deeper nesting.

EDIT

Regarding the question in the comments, you could use

Map[Mean, {#}, {-3}] /. {{a__List}} :> {a} &

to ensure that the result is always a list of lists (which would again work for deeper nestings).

Map[Mean, {#}, {-3}] /. {{a__List}} :> {a} & @ ccmppp1
{{75.9719, 75.8324, -19.2338}, {172.545, 309.651, -113.633}, 
 {172.132,315.811, -119.802}}
Map[Mean, {#}, {-3}] /. {{a__List}} :> {a} & @ ccmppp2
{{163.186, 337.94, -121.956}}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much! It worked. Also, I need to plot the point using the List Point Plot 3D command is there any way that I have the output in double braces (i.e {{163.186, 337.94, -121.956}}) so that I can plot it as well. It should be noticed that the first one does not need extra braces to plot. $\endgroup$ – Mehdi Ebadi Jul 15 at 17:32
  • 1
    $\begingroup$ @MehdiEbadi - you don't need extra brackets if you use Show, i.e., Show[ListPointPlot3D[ccmppp1, PlotRange -> All, PlotStyle -> Opacity[0.25]], Graphics3D[{Red, AbsolutePointSize[6], Point[trcentrpp1]}]] (note that Opacity is needed since two of the means are buried in a cluster) and Show[ListPointPlot3D[ccmppp2, PlotRange -> All], Graphics3D[{Red, AbsolutePointSize[6], Point[trcentrpp2]}]]. Point can accept either a coordinate or a list of coordinates. $\endgroup$ – Bob Hanlon Jul 15 at 17:54
  • $\begingroup$ Than you again Hausdorff and Bob. I have a quick question! How do you know that you should use level {-3} in Map? $\endgroup$ – Mehdi Ebadi Jul 15 at 18:20
  • 1
    $\begingroup$ You can see the depths of objects in an expression using TreeForm. {-3} just means that the function will be mapped to everything at the third lowest level. To see how that works in practice you can play around with the level i in Map[f, ccmppp1, {-i}] and see what f gets applied to. $\endgroup$ – Hausdorff Jul 15 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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