0
$\begingroup$

The output of a part of my scripts can be a list of list (e.g ccmppp1) or a list (e.g ccmppp2). I am using the following script to find the Root mean square of the nested list which is applied on the last element of each sublist and it worked well.

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}};


RootMeanSquare /@ (#[[All, -1]] & /@ ccmppp1)

However, when my output is a list (e.g ccmppp2) the above script gives the wrong output. 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?

$\endgroup$
2
  • $\begingroup$ I tried but it did not work. $\endgroup$ Jul 16, 2020 at 0:44
  • $\begingroup$ For the first one RootMeanSquare /@ Last /@ Transpose /@ ccmppp1 or RootMeanSquare /@ Map[Last, ccmppp1, {2}] and for the second one Last /@ ccmppp2 // RootMeanSquare $\endgroup$ Jul 16, 2020 at 1:05

2 Answers 2

1
$\begingroup$
rootMeanSq[list_] := If[MatrixQ@list, {RootMeanSquare[Last /@ list]}, 
RootMeanSquare /@ Last /@ Transpose /@ list]

 rootMeanSq[ccmppp1]
 rootMeanSq[ccmppp2]

{54.7358, 113.669, 119.812}

{122.696}

Or

rootMeanSq /@ {ccmppp1, ccmppp2}

{{54.7358, 113.669, 119.812}, {122.696}}

$\endgroup$
1
$\begingroup$

Some possibilities:

rms1[data_] := RootMeanSquare /@ Level[data, {-3}][[All, All, 3]]
rms1[ccmppp1]
rms1[ccmppp2]

{54.7358,113.669,119.812}

{122.696}

rms2[data_] := Map[RootMeanSquare[#[[All, 3]]] &, data, {-3}]
rms2[ccmppp1]
rms2[ccmppp2]

{54.7358,113.669,119.812}

122.696

Of the two, I would prefer the first one as it returns a list in either case. This way, even if you only have a list of 1 element with ccmppp2, you can still use Map and friends. The second returns a List in one case and a Real in the other case. I think this is likely to cause the exact same kind of issue in your question later on, where functions that work on the list don't work on atomic expressions, or visa versa.

$\endgroup$
2
  • $\begingroup$ Thanks a lot, MassDefect! It worked. May I ask what is the application of Level function? $\endgroup$ Jul 16, 2020 at 1:59
  • $\begingroup$ @MehdiEbadi In this case, it ensures that the data always has at least 3 levels. This allows us to use the same Part specification to extract just the third column of each data set. The practical effect in this case is like adding an extra set of curly braces around ccmppp2. $\endgroup$
    – MassDefect
    Jul 16, 2020 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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