vb.net - Byte array percentage similarity -


i kinda new in vb.net; pretty have been looking way calculate similarity of byte array. have been able determine whether equal or not, haven't figured out how calculate how similar in percentage. idea appreciated. thanks! leo.

the following function takes 2 byte arrays arguments. if arrays not same length, throws exception. otherwise, returns fraction of elements in 1 array equal element @ same position in other array.

function percentalike(array1() byte, array2() byte) double     if array1.length <> array2.length throw new argumentexception("arrays must have same length")     dim same integer     integer = 0 array1.length - 1         if array1(i) = array2(i) same += 1     next     return same / array1.length end function 

[added in response comment arrays may not same length] if need calculate percentage if arrays of different lengths, can use following function. returns number of elements in 1 array equal element @ same position in other array (ignoring elements in longer array) fraction of number of elements in longer array.

function percentalike(array1() byte, array2() byte) double     dim same integer     integer = 0 math.min(array1.length, array2.length) - 1         if array1(i) = array2(i) same += 1     next     return same / math.max(array1.length, array2.length) end function 

Popular posts from this blog