Next: , Previous: , Up: Internal Routines   [Contents][Index]


15.5.370 mean

mean(x [, mode, power=power, weights=weights, /float, /double, /keepdims, /omitnans])

By default, returns the arithmetic average of numerical array x: the sum divided by the number of summed values.

If mode is a scalar or an array with a size different from that of x, then it indicates the dimension(s) along which averages are calculated for each combination of the remaining coordinates. In that case, if also /keepdims is specified, then the dimension(s) along which the averages are calculated are set to 1 in the result. If /keepdims is not set, then such dimensions are omitted from the result, and if only a single number is returned, then it is returned as a scalar.

If mode is an array with the same number of elements as x, then each element of mode identifies the (scalar) class to which each corresponding element of x belongs, and then an average is returned for each class. The first element of the result refers to class 0 (if no class is negative), or to the most negative class.

If power is specified, then it must be an integer scalar (negative, zero, or positive), and each data value is raised to the indicated power before being passed on to the calculation of the arithmetic average.

If weights is specified, then it must be a numerical array with the same dimensions as x (or merely the same number of elements, if deviations are calculated by class), and then each of its elements indicates the weight of the corresponding element of x, and then weighted averages are returned. The weights are copied to a version with the same data type as x for the internal calculations (except that the copy is kept real even if x is complex).

If /omitnans is specified then NaN-values are omitted from the calculations. Otherwise the result is NaN for any summation over at least one NaN value. For example, mean(#nan, 3, 5, /omitnans) returns 4, and mean(#nan, 3, 5) returns NaN.

Complex numbers are supported.

The data type of the result is at least as great as the data type of x, and not less than long. If /float is specified, then the result is at least of type float. If /double is specified, then the result is of type double (or cdouble if x is complex).

Examples, with x = [[[6.0, 4, 3]],[[8, 2, 4]]] and c = [2.0, 2, 2, 2, 1, 1]:

To print the average along the first dimension (dimension 0, rows) of x:

LUX>t,mean(x,0)
      4.333333      4.666667

And along the second dimension (dimension 1, columns):

LUX>t,mean(x,1)
             7             3           3.5

To subtract the column average from each column of x:

LUX>x -= mean(x,1,/keepdims)
LUX>t,x(*,0)
            -1             1          -0.5
LUX>t,x(*,1)
             1            -1           0.5

To print the arithmetic averages of x for the classes identified by c:

LUX>t,mean(x,c)
             0             3          5.25

Class 0 yields 0 because there are no elements 0 in c. Class 1 yields the arithmetic average (3) of elements 2, 4, and class 2 yield the average (5.25) of elements 6, 4, 3, 8.

To calculate the average of x^2:

LUX>t,mean(x,power=2)
      48.33333

In general, mean(x,power=p) is equivalent to mean(x^p), but the first form does not generate a temporary variable for storage of x^p and is generally faster because it can take advantage of the exponent being integral.

Beware that float/cfloat numbers have only about 6 significant digits (on a 32-bit machine), so that the relative accuracy of float/cfloat results from total may be as bad as 1e-6*sqrt(n) where n is the number of data points! When in doubt, use total(x, /double) or dsum, which have a relative accuracy about a thousand million times better than float/cfloat total.

See also: hist, mean, dsum, fsum, total, sdev


Next: , Previous: , Up: Internal Routines   [Contents][Index]