Skip to content

compute hamming

kgryte edited this page May 12, 2015 · 1 revision

Computes the Hamming distance between two sequences of equal length.

var a = 'beep',
	b = 'boop';

var dist = compute.hamdist( a, b );
// returns 2

var c = [ 4, 2, 3, 4 ],
	d = [ 2, 4, 3, 1 ];

var dist = compute.hamdist( c, d );
// returns 3

To compute the Hamming distance between nested array values, provide an accessor function for accessing array values.

var a = [
	{'x':4},
	{'x':2},
	{'x':3},
	{'x':4}
];

var b = [
	[1,2],
	[2,4],
	[3,3],
	[4,1]
];

function getValue( d, i, j ) {
	if ( j === 0 ) {
		return d.x;
	}
	return d[ 1 ];
}

var dist = compute.hamdist( a, b, getValue );
// returns 3
Clone this wiki locally