% binmap - linear discretisation map from bin centres
%
% binmap :: real~'first bin centre', real~'last bin centre', N:natural~'num bins'->dmap(N).
function M=binmap(min,max,N)

	dx=(max-min)/(N-1);
	rdx=1/dx;
	M=dmap(N,@map,@revmap);

	function I=map(X)
		I=1+round(rdx*(X-min));
		I(I<1)=-inf;
		I(I>N)=inf;
	end

	function X=revmap(I)
		I=shiftdim(shiftdim(I),-1);
		X1=min+(I-1.5)*dx;
		X2=min+(I-0.5)*dx;
		X=cat(1,X1,X2);
	end
end
