AVG Function

Standard Library function that returns the average value of an array. Optionally, it can return the average value of one dimension of an array.

enabled.

Usage

    result = AVG(array [, dim])

Input Parameters

array—Array that is averaged. Array may be any data type except string.

dim—(optional) The specific dimension of array that will be averaged. Must be a number that is in the range 0 dim < n, where n is the number of dimensions in array.

Returned Value

result—The average value of array. If dim is not specified, result will be of floating-point type; otherwise, it will be of the same data type as array. If dim is specified, result is an array containing the average values for all elements of the specified dimension.

Keywords

None.

Discussion

AVG is defined as:

where n is the number of elements in the array x.

The optional parameter dim allows you to find the average values for one dimension of array rather than the whole array. The first dimension in the array is denoted by 0, the second dimension by 1, and so on.

If the dimension you specify is not valid for array, the input array is returned as the result.

Example 1

array = INTARR(3, 4)
array(*, 0) = [5, 7, 9]
array(*, 1) = [2, 8, 5]
array(*, 2) = [3, 4, 8]
array(*, 3) = [3, 3, 3]
PRINT, AVG(array)
; PV-WAVE prints: 5.00000
PRINT, AVG(array, 0)
; PV-WAVE prints: 7    5    5    3
PRINT, AVG(array, 1)
; PV-WAVE prints: 3    5    6 

Example 2

When AVG is called with the dimension parameter, the result is an array with the dimensions of the input array, except for the dimension specified. In this case, each element of the result is the average of the corresponding vector in the input array. For example, if Array has dimensions of (3,4,5), then the command:

avg_dim = AVG(array, 1)

is equivalent to these commands:

avg_dim = FLTARR(3, 5)
FOR j = 0,4 DO BEGIN
   FOR i = 0,2 DO BEGIN
      avg_dim(i,j) = TOTAL(array(i,*,j)) / 4.
   ENDFOR
ENDFOR