简体   繁体   English

查找最接近最大值的数组条目的20%

[英]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. 我试图找到最接近最大值的20%的条目。 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 . 打印FFFFFFTFFF Now I would like to find the logical array FFFTFFTFFF (2 of the 10 entries are true). 现在,我想找到逻辑数组FFFTFFTFFF (10个条目中的2个为真)。 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? 我可以将其包装成一个循环,计算array > threshold的条目数array > threshold然后降低阈值,直到得到ratio*size(array) < count(array > threshold) (对于ratio = 0.2 ),但是如何选择阈值理智地? 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. 调用子例程对数组进行排序(Fortran代码在公共域中执行此操作),然后使用排序后的数组选择阈值。 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. 您无需对整个数组进行排序,在不太可能的情况下,完全排序会花费太长时间,您可以使用公共领域ORDERPACK 2.0库中的部分排序子程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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