Skip to content

compute median

kgryte edited this page May 12, 2015 · 1 revision

Computes the median of an array.

var data = [ 2, 4, 2, 7, 3 ];

var median = compute.median( data );
// returns 3

If the input array is already sorted in ascending order, set the sorted option to true.

var data = [ 2, 2, 3, 4, 7 ];

var median = compute.median( data, {
	'sorted': true	
});
// returns 3

For object arrays, provide an accessor function for accessing numeric array values

var data = [
	[1,2],
	[2,4],
	[3,2],
	[4,7],
	[5,3]
];

function getValue( d ) {
	return d[ 1 ];
}

var median = compute.median( data, {
	'accessor': getValue
});
// returns 3
Clone this wiki locally