ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

How can I find approximate matches of numbers?

(1/3) > >>

kalos:
Hello!

I have two lists with numbers. How can I find how many pairs there are between those two lists (ie similarity), where each pair is the number from second list that matches +/-5% to a number in the first list?

Thanks!

Ath:
Do you have a set of example lists? And point out how you'd like things to match/group?

KodeZwerg:
I dont know if i understood correct, if you want to know index positions of same values, heres a Delphi Snippet that does such, hope it helps.

--- Code: Delphi ---procedure TForm148.Button2Click(Sender: TObject);type  TIntArray = array of Integer;   procedure CountOccurrences(const List1, List2: TStrings; var Result: TIntArray);  var    i, CurIndex: Integer;  begin    for i := 0 to List2.Count - 1 do    begin      CurIndex := List1.IndexOf(List2[i]);      if CurIndex >= 0 then        Result[CurIndex] := Succ(Result[CurIndex]);    end;  end; var  TempList: TIntArray;  i: Integer;begin  SetLength(TempList, ListBox1.Items.Count);  CountOccurrences(ListBox1.Items, ListBox2.Items, TempList);  for i := Low(TempList) to High(TempList) do    ShowMessage(ListBox1.Items[i] + ': ' + IntToStr(TempList[i]));  SetLength(TempList, 0);end;

wraith808:
Well, he wants to throw in a %match, so that wouldn't do it. 

There's also the matter of what kinds of numbers are in the lists.  And whether he wants individual matches, i.e. if you have 95 in one list and 100 and 105 in the other, would it be able to be 2 pairs?  Or only match the first one?  And if one is matches in the second list, is it gone for matches?

The spec for this is pretty non-specific.  :-\

p3lb0x:

--- Code: C# ---class Program    {        static void Main(string[] args)        {            List<float> A = new List<float> {};            List<float> B = new List<float> {};             FillListWithRandNumbers(A, 20);            FillListWithRandNumbers(B, 20);             var approximateNumbers = ApproxNumbers(A, B, 0.05f);              Console.WriteLine("List A: ");            foreach (var num in A)            {                Console.WriteLine(num);            }            Console.WriteLine();             Console.WriteLine("List B: ");            foreach (var num in B)            {                Console.WriteLine(num);            }            Console.WriteLine();             Console.WriteLine("Numbers within 5% of numbers in list A");            foreach (var tup in approximateNumbers)            {                Console.WriteLine(tup.Item1 + " " + tup.Item2);            }             Console.WriteLine("Match amount: " + approximateNumbers.Count);         }         static void FillListWithRandNumbers(List<float> list, int amount = 100)        {            Random r = new Random(DateTime.Now.Millisecond);            int range = 100;            for (var i = 0; i < amount; ++i)            {                list.Add((float)r.NextDouble() * range);            }        }         static List<Tuple<float, float>> ApproxNumbers(List<float> a, List<float> b, float tolerance)        {            List<Tuple<float, float>> ret = new List<Tuple<float, float>>();             foreach (var num1 in a)            {                foreach (var num2 in b)                {                    if (Math.Abs(num1 - num2) < num1 * tolerance)                    {                        ret.Add(new Tuple<float, float>(num1, num2));                    }                }            }             return ret;        }    }

Something like this?

Navigation

[0] Message Index

[#] Next page

Go to full version