Here is the following issue I am trying to solve. Computing the number of distinct elements from the following sequence within a tolerance:
var l = new List<double>() { 0, 0 1e-7, 0 2e-7, 1 - 1e-7, 1 };
The list is sorted, and all values are distinct (using the default equality comparer to compare values).
How can I count the number of unique values within a particular tolerance value:
static bool IsWithinTolerance(double x, double y)
{
return Math.Abs(x - y) < 1e-6;
}
CodePudding user response:
You can define a dictionary that stores your results:
var result = new Dictionary<double,List<double>>();
Then you loop over the list, and check if the dictionary contains a key which is in tolerance with your list item. If so, you add the item to the list in the dictionary with this key. If not, you add a new list to the dictionary:
foreach(var i in l)
{
double? key = result.Keys.Any(x => IsWithinTolerance(x,i)) ? result.Keys.First(x => IsWithinTolerance(x,i)) : default(double?);
if(key == null)
{
result.Add(i, new List<double> { i });
}
else
{
result[key.Value].Add(i);
}
}
Warning: this code does not solve the problems that Evk and canton7 noted in their comments to the question. Only use it if your clusters are clearly to be recognized and the members of each cluster are very close to each other.
Online demo: https://dotnetfiddle.net/MkF1QX