1 | function indd=match(x,y, tol,bro)
|
---|
2 | % MATCH function INDD=MATCH(X, Y, TOL, BRO) find the closest
|
---|
3 | % values to X in vector Y with tolerance TOL. If TOL is omitted, the
|
---|
4 | % default value of 1e-6 will be used. CAUTION: Using too small tolerance may
|
---|
5 | % exclude some points from results (run example below with tol=0.005)
|
---|
6 | % BRO=0 by default and can also be omitted
|
---|
7 | % If BRO=0, then all indices of values of Y within the tolerance TOL vill be
|
---|
8 | % included in the output. If BRO=1 then one (last or first) value within each range
|
---|
9 | % will be reported. If BRO>1 then one minimum value for each range
|
---|
10 | % will be included in output. Value of BRO defines indices range for
|
---|
11 | % minimum matching value search (rule of thumb- the coarser your TOL
|
---|
12 | % the bigger BRO should be. Use BRO= twice the maximum number of
|
---|
13 | % matching values for each matching point)
|
---|
14 | % (Run example below with a=match(val,y,tol, 1);
|
---|
15 | % and tol=0.01; to compare outputs)
|
---|
16 | % For example:
|
---|
17 | % xx=0:0.01:35; y=sin(xx) + cos(xx ./3);
|
---|
18 | % plot(xx,y); grid; hold on;
|
---|
19 | % val=1.04; tol=0.05;
|
---|
20 | % a=match(val,y,tol); plot(xx(a),y(a),'r.'); % All matching values
|
---|
21 | % aa=match(val,y,tol,1); plot(xx(aa),y(aa),'ms'); % Single value per range
|
---|
22 | % aaa=match(val,y,tol,20); plot(xx(aaa),y(aaa),'go'); % Min value
|
---|
23 | %
|
---|
24 | % see also LMIN,LMAX, LMAX_PW, LMIN_PW
|
---|
25 |
|
---|
26 | % Sergei Koptenko, Applied Acoustic Technologies, Toronto, Canada
|
---|
27 | % sergei.koptenko@sympatico.ca, March/22/2003
|
---|
28 |
|
---|
29 | if (nargin<4), bro=0;
|
---|
30 | else, bro=round(bro);
|
---|
31 | end; % set the default broadness
|
---|
32 | if (nargin<3), tol=1e-6; bro=0; % set the default tolerance
|
---|
33 | end;
|
---|
34 |
|
---|
35 | [rrow,ccol]=size(y);
|
---|
36 | if (ccol==1), y=y'; end
|
---|
37 | [rrow,ccol]=size(y);
|
---|
38 | if (rrow>1) || (rrow+ccol==2), disp('ERR: Y must be a vector!'); indd=[]; return; end
|
---|
39 | if ( x>max(y)) || (x< min(y) ),
|
---|
40 | disp('ERR: X is outside of range of Y!');
|
---|
41 | indd=[]; return;
|
---|
42 | end
|
---|
43 |
|
---|
44 | indd= find(xor(((x+tol) >=y), ((x-tol)<=y)) ==0); %get all indices within the tolerance
|
---|
45 |
|
---|
46 | if bro>0,
|
---|
47 | difi= diff(indd); % find index difference
|
---|
48 | fdif= find(~(difi-1)); % find neigbouring indices
|
---|
49 | ddff=indd(fdif); % get neigbouring indices
|
---|
50 | %----------remove neigbouring indices-------------------
|
---|
51 | for k1=1:length(ddff),
|
---|
52 | aa = find(~(indd - ddff(k1)));
|
---|
53 | indd(aa)=0;
|
---|
54 | end %k1
|
---|
55 | bb=sort(indd);
|
---|
56 | indd=bb(find(bb));
|
---|
57 | %----------find closest value for each range-------------------
|
---|
58 | if bro>1,
|
---|
59 | innd=1;
|
---|
60 | for j3=1:length(indd),
|
---|
61 | j4=indd(j3)-bro;
|
---|
62 | j5=indd(j3)+bro;
|
---|
63 | dj4=0;
|
---|
64 | if j4<1, dj4=-j4 +1;
|
---|
65 | j4=1;
|
---|
66 | end
|
---|
67 | if j5>ccol, j5=ccol;end
|
---|
68 | [vv,innd(j3)] = min(abs( y(j4:j5) -x ));
|
---|
69 | innd(j3)=innd(j3) + dj4;
|
---|
70 | end %j3
|
---|
71 | indd=indd + innd - bro-1;
|
---|
72 | end
|
---|
73 | end
|
---|
74 | if ~isempty(indd),
|
---|
75 | if indd(1)<1, indd(1)=1; end
|
---|
76 | if indd(end)>ccol, indd(end)=ccol; end
|
---|
77 | end
|
---|
78 | return
|
---|