Matrix Calculations
Matrix Threshold Count
Masked Sum
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.
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
values ← [[2, 8, 6], [5, 3, 9]]
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 5values this step[[2, 8, 6], [5, 3, 9]]valuesthreshold ← 5
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 510high_count = count(values > threshold)values this step5thresholdhigh_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 > thresholdhigh_total ← 23
10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_totalvalues this step23high_total[8, 6, 9]selected valuesprint '(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_demooutput3 23values this step3high_count23high_total
values ← [[2, 8, 6], [5, 3, 9]]
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 3values this step[[2, 8, 6], [5, 3, 9]]valuesthreshold ← 3
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 310high_count = count(values > threshold)values this step3thresholdhigh_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 > thresholdhigh_total ← 28
10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_totalvalues this step28high_total[5, 8, 6, 9]selected valuesprint '(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_demooutput4 28values this step4high_count28high_total
values ← [[2, 8, 6], [5, 3, 9]]
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 8values this step[[2, 8, 6], [5, 3, 9]]valuesthreshold ← 8
8values = reshape([2, 5, 8, 3, 6, 9], [2, 3])9threshold = 810high_count = count(values > threshold)values this step8thresholdhigh_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 > thresholdhigh_total ← 9
10high_count = count(values > threshold)11high_total = sum(values, mask=values > threshold)12print '(I0, 1X, I0)', high_count, high_totalvalues this step9high_total[9]selected valuesprint '(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_demooutput1 9values 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.