COUNT(MASK, DIM, KIND)

Purpose

Counts the number of true array elements in an entire logical array, or in each vector along a single dimension. Typically, the logical array is one that is used as a mask in another intrinsic.

Class

Transformational function

Argument type and attributes

MASK
An INTENT(IN) LOGICAL array
DIM (optional)
An INTENT(IN) INTEGER scalar. Its value must be in the range 1 ≤ DIM ≤ rank(MASK). The corresponding actual argument must not be an optional dummy argument.
Fortran 2003 begins KIND (optional)
An INTENT(IN) scalar INTEGER. The actual argument corresponding to KIND must be a constant expression.

Result value

If DIM is present, the result is an integer array of rank rank(MASK)-1. If DIM is missing, or if MASK has a rank of one, the result is a scalar of type integer.

Fortran 2003 begins If KIND is present, the kind of the result is that specified by the value of KIND; otherwise, the KIND type parameter is that of default integer type. Fortran 2003 ends

Each element of the resulting array (R(s1, s2, …, s(DIM-1), s(DIM+1), …, sn)) equals the number of elements that are true in MASK along the corresponding dimension (s1, s2, …, s(DIM-1), :, s(DIM+1), …, sn).

If MASK is a zero-sized array, the result equals zero.

Examples

! A is the array | T F F |, and B is the array | F F T |
!                | F T T |                     | T T T |

! How many corresponding elements in A and B
! are equivalent?
    RES = COUNT(A .EQV. B)        ! result RES is 3

! How many corresponding elements are equivalent
! in each column?
    RES = COUNT(A .EQV. B, DIM=1) ! result RES is (0,2,1)

! Same question, but for each row.
    RES = COUNT(A .EQV. B, DIM=2) ! result RES is (1,2)