Calculates the sum of selected elements in an array.
Transformational function
If DIM is present, the result is an array of rank rank(ARRAY)-1, with the same data type and kind type parameter as ARRAY. If DIM is missing, or if MASK has a rank of one, the result is a scalar.
! Sum all the elements in an array.
RES = SUM( (/2, 3, 4 /) )
! The result is 9 because (2+3+4) = 9
! 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
! 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