TOTAL Function

Sums the elements of an input array.

enabled.

Usage

result = TOTAL(array)

Input Parameters

arrayThe array that is totalled. Can be of any data type except string.

Returned Value

result—A scalar value equal to the sum of all the elements of array. If the Dimension keyword is used, then result has the structure of the input array, but with the specified dimensions collapsed. If the Dimension keyword is used and array is a scalar value, the return value is also a scalar.

Keywords

Dimension—A scalar or array of integers (n ³ 0) that specifies the dimension(s) over which to operate.

Use_Double—If the Use_Double keyword is set then the return value from total is of double data type.

Use_Long—If the Use_Long keyword is set then the return value from total is a long.

Note:

Both keywords must not be used at the same time.

Discussion

If array is of double-precision floating-point or complex data type, the result is of the same type. If array is any other data type, TOTAL returns single-precision floating-point.

The Dimension keyword lets you sum the elements across one or more dimensions of the input array. The following examples best illustrate the use of Dimension.

Note:

If the input is of type byte, integer, or long, TOTAL performs its summation in long arithmetic. This is faster than performing the summation in floating-point arithmetic. To sum an array of longs with floating-point arithmetic, you must first convert the long values to double precision.

Example 1

In this example, TOTAL is used to compute the sums of all elements in various rows and columns of a 3-by-2 integer array.

; Create a 3-by-2 integer array. Each element has a value
; equal to its one-dimensional subscript.
a = INDGEN(3, 2) 
PM, a
; PV-WAVE prints the following:
; 0     3
; 1     4
; 2     5
PRINT, "The sum of the elements in the first column:"
PM, TOTAL(a(*, 0))
; PV-WAVE prints: 3.00000
PRINT, "The sum of the elements in the second row:"
PM, TOTAL(a(1, *))
; PV-WAVE prints: 5.00000
PRINT, "The sum of all elements in the array:"
PM, TOTAL(a)
; PV-WAVE prints: 15.0000
PRINT, "The sum of elements across dimension 0:"
PM, TOTAL(a, Dimension=0)
; PV-WAVE prints the following:
; 3.0000    12.0000
PRINT, "The sum of elements across dimension 1:"
PM, TOTAL(a, Dimension=1)
; PV-WAVE prints: 
;  3.00000
;  5.00000
;  7.00000
PRINT, "The sum of elements on the diagonal:"
PM, TOTAL(a, Dimension=[0, 1])
; PV-WAVE prints: 4.00000

Example 2

a = INDGEN(4, 4)    &    PM, a
; PV-WAVE prints the following:
; 0       4       8      12
; 1       5       9      13
; 2       6      10      14
; 3       7      11      15
PM, TOTAL(a, Dim=0)
; PV-WAVE prints: 6.00000      22.0000      38.0000      54.0000
PM, TOTAL(a, Dim=1)
; PV-WAVE prints the following:
; 24.0000
; 28.0000
; 32.0000
; 36.0000
PM, TOTAL(a, Dim=[0,1])
; PV-WAVE prints: 30.0000
a = INDGEN(2, 2, 4)    &    PM, a
; PV-WAVE prints the following:
; 0       2
; 1       3
; 4       6
; 5       7
; 8      10
; 9      11
; 12      14
; 13      15
PM, TOTAL(a, Dim=2)
; PV-WAVE prints the following:
; 24.0000      32.0000
; 28.0000      36.0000
PM, TOTAL(a, Dim=[0,1])
; PV-WAVE prints the following:
; 3.00000
; 11.0000
; 19.0000   
; 27.0000
a = INTARR(5, 10, 5, 10, 5)
INFO, TOTAL(a, Dim=1)
; PV-WAVE Prints: <Expression>  FLOAT  = Array(5, 1, 5, 10, 5)
INFO, TOTAL(a, Dim=[0,2])
; PV-WAVE Prints: <Expression>  FLOAT  = Array(1, 10, 1, 10, 5)

See Also

ARRAYTRACE, AVG, MAX, MEDIAN, MIN, SIZE