The COUNT function counts the number of true elements in a logical mask either for an entire mask or along a given dimension of the mask.

## Syntax

COUNT (mask [,dim])

*mask* is an INTENT(IN) array of type LOGICAL. It must not be scalar.

*dim* is an INTENT(IN) scalar of type INTEGER with a value within the range *dim* ≤ *n**n* is the rank of mask. The corresponding actual argument must not be an optional dummy argument.

The result is of type default INTEGER. Its value and rank are computed as follows:

- The function will return a scalar logical value if
*mask*has rank one, or if*dim*is absent. The result is the number of elements for which*mask*is true. - The function will return a logical array of rank n-1 if
*dim*is present and*mask*has rank two or greater. The resulting array is of shape (*d*) where (_{1},d_{2}, ...,d_{dim-1},d_{dim+1}, ...,d_{n}*d*) is the shape of_{1},d_{2}, ...,d_{n}*mask*and*n*is the rank of*mask*. The result is the number of true elements for each corresponding vector in*mask*.

## Example

integer,dimension(2,3) :: a,b a=reshape((/1,2,3,4,5,6/),shape(a)) write(*,'(2i3)') a ! writes 1 2 ! 3 4 ! 5 6 b=reshape((/1,2,3,5,6,4/), (/2,3/)) write(*,'(2i3)') b ! writes 1 2 ! 3 5 ! 6 4 write(*,*) count(a==b) ! writes 3 write(*,*) count(a==b,dim=1) ! writes 2 1 0 write(*,*) count(a==b,dim=2) ! writes 2 1