diff general/discretise/linmap.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/discretise/linmap.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,30 @@
+function M=linmap(p1,p2,p3)
+% linmap - Create linearly spaced discretisation map
+%
+% linmap :: Min:real, Max:real, N:natural -> dmap(N).
+% linmap :: [Min,Max]:[[1,2]], N:natural -> dmap(N).
+%
+% Parameters work almost but not quite like linspace: the
+% third parameter is the number of GAPS, not the number of
+% points, defining the map, So for a map with edges at 0:10,
+% use linmap(0,10,10). To get the same edges from linspace,
+% you would need linspace(0,10,11).
+
+% unpack parameters
+if nargin==2, min=p1(1); max=p1(2); bins=p2;
+else min=p1; max=p2; bins=p3; end
+
+dx=(max-min)/bins;
+M=dmap(bins,@(t)map(bins,min,1/dx,t),@(t)revmap(min,dx,t));
+
+function I=map(N,min,k,X)
+	I=1+floor(k*(X-min));
+	I(I<1)=-inf;
+	I(I>N)=inf;
+
+function X=revmap(min,ik,I)
+	I=shiftdim(shiftdim(I),-1);
+	X1=min+(I-1)*ik;
+	X2=min+(I)*ik;
+	X=cat(1,X1,X2);
+