Fortran Wiki


Counts the number of .true. elements in a logical mask, or, if the dim argument is supplied, counts the number of elements along each row of the array in the dim direction. If the array has zero size, or all of the elements of mask are .false., then the result is 0.


Fortran 95 and later, with kind argument Fortran 2003 and later


Transformational function


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


  • mask - The type shall be logical.
  • dim - (Optional) The type shall be integer.
  • kind - (Optional) An integer initialization expression indicating the kind parameter of the result.

Return value

The return value is of type integer and of kind kind. If kind is absent, the return value is of default integer kind. If dim is present, the result is an array with a rank one less than the rank of array, and a size corresponding to the shape of array with the dim dimension removed.


program test_count
    integer, dimension(2,3) :: a, b
    logical, dimension(2,3) :: mask
    a = reshape( (/ 1, 2, 3, 4, 5, 6 /), (/ 2, 3 /))
    b = reshape( (/ 0, 7, 3, 4, 5, 8 /), (/ 2, 3 /))
    print '(3i3)', a(1,:)
    print '(3i3)', a(2,:)
    print *
    print '(3i3)', b(1,:)
    print '(3i3)', b(2,:)
    print *
    mask =
    print '(3l3)', mask(1,:)
    print '(3l3)', mask(2,:)
    print *
    print '(3i3)', count(mask)
    print *
    print '(3i3)', count(mask, 1)
    print *
    print '(3i3)', count(mask, 2)
end program test_count

category: intrinsics