Whole-matrix comparisons produce a logical mask that can drive both count and masked sum.

Program

Play the program to change the threshold and see the selected matrix values change.

threshold
matrix_threshold_count.f90
Replay: real traced execution (multi-file project)
program matrix_threshold_count_demo
    implicit none
    integer :: values(2, 3)
    integer :: threshold
    integer :: high_count
    integer :: high_total

    values = reshape([2, 5, 8, 3, 6, 9], [2, 3])
    threshold = 5
    high_count = count(values > threshold)
    high_total = sum(values, mask=values > threshold)
    print '(I0, 1X, I0)', high_count, high_total
end program matrix_threshold_count_demo
program matrix_threshold_count_demo
    implicit none
    integer :: values(2, 3)
    integer :: threshold
    integer :: high_count
    integer :: high_total

    values = reshape([2, 5, 8, 3, 6, 9], [2, 3])
    threshold = 3
    high_count = count(values > threshold)
    high_total = sum(values, mask=values > threshold)
    print '(I0, 1X, I0)', high_count, high_total
end program matrix_threshold_count_demo
program matrix_threshold_count_demo
    implicit none
    integer :: values(2, 3)
    integer :: threshold
    integer :: high_count
    integer :: high_total

    values = reshape([2, 5, 8, 3, 6, 9], [2, 3])
    threshold = 8
    high_count = count(values > threshold)
    high_total = sum(values, mask=values > threshold)
    print '(I0, 1X, I0)', high_count, high_total
end program matrix_threshold_count_demo
  1. values ← [[2, 8, 6], [5, 3, 9]]

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 5
    values this step[[2, 8, 6], [5, 3, 9]]values
  2. threshold ← 5

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 510high_count = count(values > threshold)
    values this step5threshold
  3. high_count ← 3

    9threshold = 510high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)
    values this step3high_count[F, T, T, F, F, T]values > threshold
  4. high_total ← 23

    10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_total
    values this step23high_total[8, 6, 9]selected values
  5. print '(I0, 1X, I0)', high_count, high_total

    11    high_total = sum(values, mask=values > threshold)12    print '(I0, 1X, I0)', high_count, high_total13end program matrix_threshold_count_demo
    output3 23
    values this step3high_count23high_total
  1. values ← [[2, 8, 6], [5, 3, 9]]

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 3
    values this step[[2, 8, 6], [5, 3, 9]]values
  2. threshold ← 3

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 310high_count = count(values > threshold)
    values this step3threshold
  3. high_count ← 4

    9threshold = 310high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)
    values this step4high_count[F, T, T, F, T, T]values > threshold
  4. high_total ← 28

    10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_total
    values this step28high_total[5, 8, 6, 9]selected values
  5. print '(I0, 1X, I0)', high_count, high_total

    11    high_total = sum(values, mask=values > threshold)12    print '(I0, 1X, I0)', high_count, high_total13end program matrix_threshold_count_demo
    output4 28
    values this step4high_count28high_total
  1. values ← [[2, 8, 6], [5, 3, 9]]

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 8
    values this step[[2, 8, 6], [5, 3, 9]]values
  2. threshold ← 8

    8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 810high_count = count(values > threshold)
    values this step8threshold
  3. high_count ← 1

    9threshold = 810high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)
    values this step1high_count[F, F, F, F, F, T]values > threshold
  4. high_total ← 9

    10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_total
    values this step9high_total[9]selected values
  5. print '(I0, 1X, I0)', high_count, high_total

    11    high_total = sum(values, mask=values > threshold)12    print '(I0, 1X, I0)', high_count, high_total13end program matrix_threshold_count_demo
    output1 9
    values this step1high_count9high_total
logical mask `values > threshold` compares every matrix element.
count `count(mask)` counts the `.true.` positions in the mask.
masked sum `sum(values, mask=mask)` adds only selected elements.