简体   繁体   中英

Find 20% of array entries that are closest maximum value

I'm trying to find 20% of the entries that are closest to the maximum. Consider this program

program max_find
    implicit none
    double precision, dimension(10) :: array
    array = [4.0, 7.0, 6.0, 9.0, 3.0, 2.0, 10.0, 5.0, 1.0, 8.0]
    print *, array == maxval(array)
end program

which prints FFFFFFTFFF . Now I would like to find the logical array FFFTFFTFFF (2 of the 10 entries are true). I could wrap this in a loop, count the number of entries in array > threshold and lower the threshold until I get ratio*size(array) < count(array > threshold) (for ratio = 0.2 ), but how to choose the threshold sensibly? Is there a better approach?

Call a subroutine to sort the array (Fortran codes to do that are in the public domain) and use the sorted array to choose the threshold. You don't need to sort the entire array, and in the unlikely case that full sorting takes too long, you can use a partial ranking subroutine from the public domain ORDERPACK 2.0 library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM