Suppose a single data set contains entries with different dimensions,

{RandomReal[{-1., 1.}, {2, 2}], RandomReal[{-1., 1.}, {3, 3}]}

and one wants to obtain the Mean value of 10 such data sets:

dat=Table[{RandomReal[{-1., 1.}, {2, 2}], RandomReal[{-1., 1.}, {3, 3}]}, 10];

My question is, why does Mean[dat] not work whereas Total[dat]/10 does work?

  • $\begingroup$ I'm surprised that Total[] actually worked... $\endgroup$ Jul 31, 2016 at 21:04
  • $\begingroup$ @J.M. Why? Rows are all same structure... $\endgroup$
    – ciao
    Jul 31, 2016 at 21:23
  • $\begingroup$ @ciao, it's still a ragged array, after all. But I am glad it does work. $\endgroup$ Jul 31, 2016 at 21:24
  • 2
    $\begingroup$ Help says that Mean[list] is equivalent to Total[list]/Length[list] so arguably this is a bug. $\endgroup$
    – mikado
    Jul 31, 2016 at 22:32

2 Answers 2


The observed behavior appears to caused by Mean having different requirements than Total. Or you might say it is being fussier.

The Details and Options section of the Documentation Center article on Total says

For a 2D array or matrix, Total[list] totals for each column

That means Total[list] effectively transposes data, so a truly equivalent application of Mean would be

data = Table[{RandomReal[{-1., 1.}, {2, 2}], RandomReal[{-1., 1.}, {3, 3}]}, 10];
Mean /@ Transpose[data]
{{{-0.0841252, 0.30287}, {0.188753, 0.258856}}, 
 {{-0.219886, 0.0948545, 0.310493}, {-0.127705, 0.0855042, 0.508691}, 
  {0.113957, 0.328553, 0.405479}}}

which is what




This seems to be one of those cases where the documentation tries to illustrate with a simple analogy which unfortunately is only superficially true.

I am going to quote Kuba directly as I think he expressed this quite well:

Documentation is usually correct from a perspective of a beginner who doesn't care/know context/kernels/links etc. When you try to develop something complex then those reckless sentences have major implications. Sometimes it is a matter of few tests and sometimes you waste two days on debugging and discussions here.

And quoting myself:

Quite often the documentation is not complete in describing the behavior of Mathematica functions. In my opinion a user has to accept in vivo behavior as at least co-authoritative with the documentation. I know this causes consternation for those who want a rigorous language definition but as far as I know from the beginning we have never been provided with one.

The error message itself appears to both explain the problem and clearly indicate that Mean was not intended to work this way. I far simpler example may be used to provoke it:

dat = {{{1, 2}, {3}}, {{4, 5}, {6}}};


Mean::rectt: Rectangular array expected at position 1 in Mean[{{{1,2},{3}},{{4,5},{6}}}]. >>

Total again works as desired:

{{5/2, 7/2}, {9/2}}

Mean expects a rectangular array as the message indicates. So, as I see m_goldberg just beat me to posting, we need to make sure that what is passed to Mean is a rectangular array, and we can do that by separating our dat:

Mean @ dat[[All, 1]]
Mean @ dat[[All, 2]]
{5/2, 7/2}


Or more elegantly as m_goldberg wrote, using Transpose:

Mean /@ (dat\[Transpose])
{{5/2, 7/2}, {9/2}}

Your Answer

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

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