将两个双打列表与一些容差进行比较的最快方法是什么? [英] What is the fastest way to compare two lists of doubles with some tolerance?

查看:54
本文介绍了将两个双打列表与一些容差进行比较的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个数组或双打列表,以查看一个列表中是否有任何其他列表的数字在某个容差范围内说0.005 ...最快的方法是什么?一个小例子会很棒!

谢谢!



我尝试过:



我没有尝试过任何问题,这就是为什么我要问

I want to compare two arrays or lists of doubles to see if one list has any of the other list's numbers within some tolerance say 0.005...What is the fastest way to do this? A small example would be great!
Thanks!

What I have tried:

I haven't tried anything which is why I'm asking

推荐答案

您可以尝试创建自定义扩展检查列表中带有容差的数字的方法:

You could try to create a custom made extension method to check a number in a list with tolerance:
public static class Utility
{
   public static bool HasApproachingValue(this double[] array, double value, double precision) {
      bool result = false;
      for (int i = 0; i < array.Length; i++) {
         if (Math.Abs(array[i] - value) < precision) {
            result = true;
            break;
         }
      }
      return result;
   }
}



您可以这样使用:


Which you could use like that:

double[] firstList;
double[] secondList; // These list should be initialized with values
double precision = 0.005d;
for (int i = 0; i < firstList.Length; i++) {
   if (secondList.HasApproachingValue(firstList[i], precision)) {
      // Here's a match
      break;
   }
}



最糟糕的情况是没有匹配时,因为会有(firstList.Length * secondList.Length )b / b
希望这会有所帮助。


The worst-case scenario will be when there is no match, as there will be (firstList.Length * secondList.Length) occurrences.
Hope this helps.


这是一个函数示例。这是独家的,但如果需要,你可以改变它。



Here's an example as a function. This is exclusive, but you can change that if need be.

bool IsTolerant(double[] array1, double[] array2)
{
    for (int i = 0; i < array1.Length; i++)
    {
         for (int j = 0; j < array2.Length; j++)
         {
              double dif = Math.Abs(array1[i] - array2[j]);
              if (dif < 0.005)
              {
                  return true;
              }
          }
    }

    return false;
}





编辑:误读问题。



Misread question.


这篇关于将两个双打列表与一些容差进行比较的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆