Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

COUNT

Transformational Intrinsic Function (Generic): Counts the number of true elements in an entire array or in a specified dimension of an array.

result = COUNT (mask[,dim][, kind])

mask

(Input) Must be a logical array.

dim

(Input; optional) Must be a scalar integer expression with a value in the range 1 to n, where n is the rank of mask.

kind

(Input; optional) Must be a scalar integer constant expression.

Results

The result is an array or a scalar of type integer. If kind is present, the kind parameter of the result is that specified by kind; otherwise, the kind parameter of the result is that of default integer. If the processor cannot represent the result value in the kind of the result, the result is undefined.

The result is a scalar if dim is omitted or mask has rank one. A scalar result has a value equal to the number of true elements of mask. If mask has size zero, the result is zero.

An array result has a rank that is one less than mask, and shape (d1, d2, ..., ddim-1, ddim+1, ..., dn), where (d1, d2,..., dn) is the shape of mask.

Each element in an array result equals the number of elements that are true in the one dimensional array defined by mask(s1, s2, ..., sdim-1, :, sdim+1, ..., sn).

Example

COUNT ((/.TRUE., .FALSE., .TRUE./)) has the value 2 because two elements are true.

COUNT ((/.TRUE., .TRUE., .TRUE./)) has the value 3 because three elements are true.

A is the array

  [ 1  5  7 ]
  [ 3  6  8 ]

and B is the array

  [ 0  5  7 ]
  [ 2  6  9 ].

COUNT (A .NE. B, DIM=1) tests to see how many elements in each column of A are not equal to the elements in the corresponding column of B. The result has the value (2, 0, 1) because:

  • The first column of A and B have 2 elements that are not equal.

  • The second column of A and B have 0 elements that are not equal.

  • The third column of A and B have 1 element that is not equal.

COUNT (A .NE. B, DIM=2) tests to see how many elements in each row of A are not equal to the elements in the corresponding row of B. The result has the value (1, 2) because:

  • The first row of A and B have 1 element that is not equal.

  • The second row of A and B have 2 elements that are not equal.

The following shows another example:

LOGICAL mask (2, 3)
INTEGER AR1(3), AR2(2), I
mask = RESHAPE((/.TRUE., .TRUE., .FALSE., .TRUE., &
                 .FALSE., .FALSE./),(/2,3/))
!
! mask is the array   true false false
!                     true true false
AR1 = COUNT(mask,DIM=1) ! counts true elements by
                        ! column yielding [2 1 0]
AR2 = COUNT(mask,DIM=2) ! counts true elements by row
                        ! yielding [1 2]
I = COUNT( mask)        ! returns 3

See Also