CStats:Mean


The Mean method computes the mean and standard deviation for a data sample. The sample may be a Lua table or a class object of type CImage, CArray, or CMatrix. For CImage and CMatrix objects, an optional CRect object can be used to define the points used in the calculation.

Syntax

nMean, nStdDev = CStats:Mean( table )

nMean, nStdDev = CStats:Mean( CImage, )

nMean, nStdDev = CStats:Mean( CImage, CRect )

nMean, nStdDev = CStats:Mean( CArray )

nMean, nStdDev = CStats:Mean( CArray, CRect )

nMean, nStdDev = CStats:Mean( CMatrix )

where

    table is a lua table containing the data.

    CImage, CArray, and CMatrix are class objects containing the data to measure,

    CRect is a CRect rectangle object that defines the region to measure.

    nMean and nStdDev are the mean and standard deviation of the data. On failure, 0,0 is returned.

  

Although this method returns two values, the mean and the standard deviation, you do not have to "catch" the standard deviation by assigning to a variable. For example,

m = S:Mean( T )

-- get the mean of table T.

m, sd = S:Mean( T )

-- gets the mean and standard deviation of table T.

If Mean is the last function called in the parameter list of a function, both of its return values can be used implicitly as shown in this example:

     Printf( "%lg +/- %lg, S:Mean(t) )

Here, S:Mean(t) returns both the mean value and standard deviation of the table t and the format specifies that both will be printed. The number of values used is controlled by the format parameter, hence using just "m=%lg" would print only the first return value (the mean value) and ignore the any addional parameters returned by S:Mean(t).

Instead of using this class method, the Related Global Functions TMean and TSdev can be used to compute the mean and standard deviation without creating a CStats class object.

Examples

Suppose a CImage I exists. The following script returns the mean and standard deviation of 600 pixels in the corner of the image:

S = CStats:new()

-- create a CStats object

m, sd = S:Mean( I, NewRect(1,20,1,30) )

-- computes values inside the CRect

Printf("Mean=%lg +/- %lg\n", m, sd )

-- list the result

S:delete()

-- when done with S, delete it

The following script returns the mean and standard deviation for a table of data. Note that both results returned by Mean are listed because it is the last argument for the Printf function:

S = CStats:new()

-- create a CStats object

t = { 4, 4, 2, 5, 6.4, -1.42, -12, 3.4 }

-- create some data in a table

Printf("Mean=%lg +/- %lg\n", S:Mean(t) )

-- list the results

Related Topics

CStats Class

TMean

TSdev