samer@4
|
1 function M=linmap(p1,p2,p3)
|
samer@4
|
2 % linmap - Create linearly spaced discretisation map
|
samer@4
|
3 %
|
samer@4
|
4 % linmap :: Min:real, Max:real, N:natural -> dmap(N).
|
samer@4
|
5 % linmap :: [Min,Max]:[[1,2]], N:natural -> dmap(N).
|
samer@4
|
6 %
|
samer@4
|
7 % Parameters work almost but not quite like linspace: the
|
samer@4
|
8 % third parameter is the number of GAPS, not the number of
|
samer@4
|
9 % points, defining the map, So for a map with edges at 0:10,
|
samer@4
|
10 % use linmap(0,10,10). To get the same edges from linspace,
|
samer@4
|
11 % you would need linspace(0,10,11).
|
samer@4
|
12
|
samer@4
|
13 % unpack parameters
|
samer@4
|
14 if nargin==2, min=p1(1); max=p1(2); bins=p2;
|
samer@4
|
15 else min=p1; max=p2; bins=p3; end
|
samer@4
|
16
|
samer@4
|
17 dx=(max-min)/bins;
|
samer@4
|
18 M=dmap(bins,@(t)map(bins,min,1/dx,t),@(t)revmap(min,dx,t));
|
samer@4
|
19
|
samer@4
|
20 function I=map(N,min,k,X)
|
samer@4
|
21 I=1+floor(k*(X-min));
|
samer@4
|
22 I(I<1)=-inf;
|
samer@4
|
23 I(I>N)=inf;
|
samer@4
|
24
|
samer@4
|
25 function X=revmap(min,ik,I)
|
samer@4
|
26 I=shiftdim(shiftdim(I),-1);
|
samer@4
|
27 X1=min+(I-1)*ik;
|
samer@4
|
28 X2=min+(I)*ik;
|
samer@4
|
29 X=cat(1,X1,X2);
|
samer@4
|
30
|