我目前正在创建一个简单的 java 应用程序,它读取图表上 4 个噪声值的数组,然后计算平均值并将其绘制在图表上。
我的目标是通过拒绝任何超过其他 3 个结果平均值两倍的数组读数来消除“误报”值。
例如:
Array [1] = 50
Array [2] = 48
Array [3] = 53
Array [4] = 102 < Reject this value as it is more than twice the level of the average of the other 3 results (Average = 100.7)
如何在 if 语句中实现这一点?这样它就会忘记“误报”值吗?它可以是任何可以被拒绝的数组 [x]。谢谢。
最佳答案
我的答案与 Clashsoft's 非常相似,但效率稍微高一些,因为它是 O(length) 而不是 O(length^2)。
/* Returns the index of the false alarm if exists, otherwise -1 */
public static int falseAlarm(double[] array){
int length = array.length;
double total = 0;
for (int i = 0; i < length; i++) {
total += array[i];
}
for (int i = 0; i < length; i++) {
double average = (total - array[i])/(length - 1);
if (array[i] > average * 2)
return i; // early return if false alarm is found
}
return -1;
}
关于Java 数组拒绝大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34695526/