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
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 =
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 =
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 =
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
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.