samer@4: function M=linmap(p1,p2,p3) samer@4: % linmap - Create linearly spaced discretisation map samer@4: % samer@4: % linmap :: Min:real, Max:real, N:natural -> dmap(N). samer@4: % linmap :: [Min,Max]:[[1,2]], N:natural -> dmap(N). samer@4: % samer@4: % Parameters work almost but not quite like linspace: the samer@4: % third parameter is the number of GAPS, not the number of samer@4: % points, defining the map, So for a map with edges at 0:10, samer@4: % use linmap(0,10,10). To get the same edges from linspace, samer@4: % you would need linspace(0,10,11). samer@4: samer@4: % unpack parameters samer@4: if nargin==2, min=p1(1); max=p1(2); bins=p2; samer@4: else min=p1; max=p2; bins=p3; end samer@4: samer@4: dx=(max-min)/bins; samer@4: M=dmap(bins,@(t)map(bins,min,1/dx,t),@(t)revmap(min,dx,t)); samer@4: samer@4: function I=map(N,min,k,X) samer@4: I=1+floor(k*(X-min)); samer@4: I(I<1)=-inf; samer@4: I(I>N)=inf; samer@4: samer@4: function X=revmap(min,ik,I) samer@4: I=shiftdim(shiftdim(I),-1); samer@4: X1=min+(I-1)*ik; samer@4: X2=min+(I)*ik; samer@4: X=cat(1,X1,X2); samer@4: