Calculates the sum of selected elements in an array.
Class
Transformational function
Result Value
If DIM is present, the result is an array of rank rank(ARRAY)-1, with the same data type as ARRAY. If DIM is missing, or if MASK has a rank of one, the result is a scalar.
The result is calculated by one of the following methods:
+---------------------------------Fortran 95---------------------------------+
Because both DIM and MASK are optional, various combinations of arguments are possible. When the -qintlog option is specified with two arguments, the second argument refers to one of the following:
+-----------------------------End of Fortran 95------------------------------+
Examples
Method 1:
! Sum all the elements in an array.
RES = SUM( (/2, 3, 4 /) )
! The result is 9 because (2+3+4) = 9
Method 2:
! A is the array (/ -3, -7, -5, 2, 3 /)
! Sum all elements that are greater than -5.
RES = SUM( A, MASK = A .GT. -5 )
! The result is 2 because (-3 + 2 + 3) = 2
Method 3:
! B is the array | 4 2 3 |
! | 7 8 5 |
! Sum the elements in each column.
RES = SUM(B, DIM = 1)
! The result is | 11 10 8 | because (4 + 7) = 11
! (2 + 8) = 10
! (3 + 5) = 8
! Sum the elements in each row.
RES = SUM(B, DIM = 2)
! The result is | 9 20 | because (4 + 2 + 3) = 9
! (7 + 8 + 5) = 20
! Sum the elements in each row, considering only
! those elements greater than two.
RES = SUM(B, DIM = 2, MASK = B .GT. 2)
! The result is | 7 20 | because (4 + 3) = 7
! (7 + 8 + 5) = 20