TMean


The TMean function returns the mean and standard deviation of a data collection. The collection can be a Lua table or an object of type CImage, CArray, or CMatrix. To process a variable list of values and tables, use the slower VMean function. Also see the CStats:Mean method.

Syntax

nMean, nStdDev, nStatus = TMean( table )

nMean, nStdDev, nStatus = TMean( CImage )

nMean, nStdDev, nStatus = TMean( CArray )

nMean, nStdDev, nStatus = TMean( CMatrix )

where

    table, CImage, CArray, or CMatrix contains the target data.

    nMean and nStdDev are the mean and standard deviation of the data.

    nStatus is the success code from the calculation (non-zero on failure).

  

All argument types except for the CImage class use double precision real numbers having data type "double". The CImage class supports values ranging from byte to double plus other data types. Numeric values other than double can be processed by first converting the table to a CImage. Note that you do not have to use the 2nd or 3rd return value.

The CStats:Mean method also can be used to calculate the mean. Using the CStats class has the added ability to constrain the sample to a rectangular region of a CImage or CMatrix.

Examples

The following script returns the mean and standard deviation in a table of 1 million random numbers.

t = TRand( 1000000 )

-- create a table of numbers

v, sd = TMean( t )

-- find the mean & standard deviation

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

-- list the result

  

The next script returns the mean value for pixels inside a rectangle at the corner of a CImage.

V = AttachView( "CImageView" )

-- attach the topmost image window

Assert( V and V:Count() > 0 )

-- make sure an image window is on top

R = NewRect( 1, 20, 1, 40 )

-- rectangle for corner of image

v = TMean( V:GetImage(), R )

-- use the current image in the CImageView

Printf("Mean=%lg\n", v )

-- list the result

Related Topics

Table Functions

VMean

TMedian

TSdev

CImageView Class

CStats class