MATLAB: Calculating hamming distance and return the array correspondingly to minimum value between the given possibilities

digital signal processingimage processingsignal processing

Hi guys!
Im trying to implement that gets on its input binary stream fixed 4bit array values like arr=[1 0 0 1] and returns the correspond array stream to the minmum hamming distance of each possibilities of the tuple constant type values, the typle values are three values tuple = { 1 0 0 0 ; 1 1 1 1 ; 0 0 0 0} (the tuple could be matrix 3×4 or table or an array of arrays -two dimensional array )
so for instance in this example, arr=[1 0 0 1]
I compare this arr with each possiblities of tuple by hamming distance we figure out:
for first possiblity of the tuple: input arr=1 0 0 1 compared to 1 0 0 0 , the hamming distance is 1.
for second possiblity of the tuple: input arr=1 0 0 1 compared to 1 1 1 1 , the hamming distance is 2.
for third possibility of the tuple: input arr=1 0 0 1 compared to 0 0 0 0, the hamming distance is 2.
So we see here the minmum value of the all the calculated hamming distance is 1, so the function returns the correspond array of the hamming distance 1 which it's
[1 0 0 0 ].
In addition, if there's for instance two minimum values of hamming distance values after comparison of all possibilities like 2 2 2 , so min of (2,2,2) is 2, in this case any returned array of the minmum value 2 doesn't matter , what's matter to return the correspond array of the minimum hamming distance, so in my case if all comparison of hamming distance are :
2
2
2
then returns any of { 1 0 0 0 ; 1 1 1 1 ; 0 0 0 0} would be correct ! (doesn't matter which one to return because all implicitly corresponding to the minimum hamming distance!
Another example if after calculating the hamming distances there's more than one minimum hamming distances like 3 2 2 so minmum is 2, so I return any array of the tuple that its hamming distance is 2 (doesn't matter which one ..because in my example here there's two array of the tuple having minimum hamming distance)
the tuple is constant and given inside the function itself, the function just gets as input arr -the given array for comparison , the tuple values are: tuple = { 1 0 0 0 ; 1 1 1 1 ; 0 0 0 0}
How could I implement that in matlab ? could anyone assist me to implement that in matlab?
thanks alot, I hope the problem is understandable.

Best Answer

arr=[1 0 0 1]
tuple = [ 1 0 0 0 ; 1 1 1 1 ; 0 0 0 0 ] % cell2mat({ 1 0 0 0 ; 1 1 1 1 ; 0 0 0 0})
n = sum(xor(tuple,arr),2);
[minn, r]=min(n);
best=tuple(n==minn,:)