Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 VS2010 .NET4 arrays
I have a problem, i am designing an app for a competition, and one of the main functions requires a comparison of 2 byte arrays. But I cannot compare each array element individually as both of the arrays have more than 13000 elements and they are not nessecarily in the same order. I need to compare them as a whole, and return a percentage of how much they match each other.
Does anyone have any ideas?
 
Thank you in advance!
Posted 23-Feb-12 22:24pm
Edited 23-Feb-12 22:54pm
v2
Comments
johannesnestler at 24-Feb-12 5:23am
   
A (naiv) question: If the corresponding bytes are not in the same order - how you know which byte represents the "same" in the other array? - I'm thinking in the direction of an diff algorithm, used to compare files (something like this: http://www.codeproject.com/Articles/6943/A-Generic-Reusable-Diff-Algorithm-in-C-II) - What you think?
Aleksa Krstic at 24-Feb-12 14:09pm
   
That actually helped me a lot, gave me a couple of ideas. Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use SequenceEqual for that.
http://msdn.microsoft.com/en-us/library/bb348567.aspx
 
Good luck!
  Permalink  
Comments
Aleksa Krstic at 24-Feb-12 14:08pm
   
Thank you, but I need something that returns a numerical value (e.g. 86% pecent of an arry matches the other one), not a boolean.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just an idea:
int[] bucket = new int[256];
foreach ( var b in array1 )
{
    bucket[b]++;
}
 
foreach ( var b in array2 )
{
    bucket[b]--;
}
 
double percent = 100;
for ( int i = 0; i < 256; ++i )
{
    double val = 100 * ((double)bucket[i] / array1.Length);
    percent -= Math.Abs( val );
}
 
Console.Out.WriteLine( string.Format( "array1 equals array2 to {0}%", percent ) );
 
The array bucket keeps track of how many different bytes are present in each of the source arrays.
A positive value means that a byte is x time more often in array1 than in array2. A negative value the other way around.
At the end you can calculate the percentage easily. (At least that's how it should work in my crazy brain Wink | ;) )
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Assuming you have two arrays and you compare by position, then you might use the following:
 
byte[] a = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] b = new byte[] { 0, 1, 2, 3, 3, 3, 6, 7 };
 
int n = Math.Min(a.Length, b.Length);
int m = Math.Max(a.Length, b.Length);
int c = 0;
a.Take(n).Aggregate(0, (i, e) => { if (e == b[i++]) c++; return i; });
 
Console.WriteLine("Match = {0} = {1}%", c, 100.0 * c / m);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Your question is not so clearly stated (what kind of comparison is asked for).
Maybe you are interested in correlation coefficients as used in audio/image compression?
If so, the following document might help: Correlation in Statistics and in Data Compression[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 420
1 Sergey Alexandrovich Kryukov 310
2 George Jonsson 269
3 Gihan Liyanage 255
4 OriginalGriff 245
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 24 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100