V_INTERVAL Classify X values into a set of contiguous intervals with boundaries from Y [I,F]=(X,Y,M) Usage: x=[1.6 2 3.8 0 6.5]; % test values (not necessarily monotonic) y=[1 2 3 5 6]; % define boundaries of four unequal intervals (y must be increasing) [i,f]=v_interval(x,y); % classify into intervals using default options 'eE' % i=[1 2 3 1 4] and f=[0.6 0 0.4 -1 1.5] Inputs: x(nx) Vector of test values y(ny) Vector of monotonically increasing interval boundaries: interval i is [ y(i) , y(i+1) ) m(nx) string of mode options if x(j)<y(1) 'e' extrapolate: set i(j)=1 and f(j)<0 [default] 'c' clip: set i(j)=1 and f(j)=0 'n' NaN: set i(j)=f(j)=NaN 'z' zero: set i(j)=0 and f(j)<0 if x(j)>=y(ny) 'E' set i(j)=ny-1 and f(j)>1 [default] 'C' set i(j)=ny-1 and f(j)=1 'N' set i(j)=f(j)=NaNj 'Z' set i(j)=ny and f(j)>1 Outputs: i(nx) Input x(j) lies in the interval [y(i(j)),y(i(j)+1)] f(nx) f(j)=(x(j)-y(i(j)))/(y(i(j)+1))-y(i(j))) is the fractional position of x(j) within the interval. Note that f(j) lies in the range [0,1) provided that y(1) <= x(j) < y(ny)
0001 function [i,f]=v_interval(x,y,m) 0002 %V_INTERVAL Classify X values into a set of contiguous intervals with boundaries from Y [I,F]=(X,Y,M) 0003 % 0004 % Usage: x=[1.6 2 3.8 0 6.5]; % test values (not necessarily monotonic) 0005 % y=[1 2 3 5 6]; % define boundaries of four unequal intervals (y must be increasing) 0006 % [i,f]=v_interval(x,y); % classify into intervals using default options 'eE' 0007 % % i=[1 2 3 1 4] and f=[0.6 0 0.4 -1 1.5] 0008 % 0009 % Inputs: x(nx) Vector of test values 0010 % y(ny) Vector of monotonically increasing interval boundaries: interval i is [ y(i) , y(i+1) ) 0011 % m(nx) string of mode options 0012 % if x(j)<y(1) 0013 % 'e' extrapolate: set i(j)=1 and f(j)<0 [default] 0014 % 'c' clip: set i(j)=1 and f(j)=0 0015 % 'n' NaN: set i(j)=f(j)=NaN 0016 % 'z' zero: set i(j)=0 and f(j)<0 0017 % if x(j)>=y(ny) 0018 % 'E' set i(j)=ny-1 and f(j)>1 [default] 0019 % 'C' set i(j)=ny-1 and f(j)=1 0020 % 'N' set i(j)=f(j)=NaNj 0021 % 'Z' set i(j)=ny and f(j)>1 0022 % 0023 % Outputs: i(nx) Input x(j) lies in the interval [y(i(j)),y(i(j)+1)] 0024 % f(nx) f(j)=(x(j)-y(i(j)))/(y(i(j)+1))-y(i(j))) is the fractional position of x(j) within the interval. 0025 % Note that f(j) lies in the range [0,1) provided that y(1) <= x(j) < y(ny) 0026 % 0027 0028 % Copyright (C) Mike Brookes 2025 0029 % Version: $Id: v_importsii.m 10865 2018-09-21 17:22:45Z dmb $ 0030 % 0031 % VOICEBOX is a MATLAB toolbox for speech processing. 0032 % Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html 0033 % 0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0035 % This program is free software; you can redistribute it and/or modify 0036 % it under the terms of the GNU Lesser General Public License as published by 0037 % the Free Software Foundation; either version 3 of the License, or 0038 % (at your option) any later version. 0039 % 0040 % This program is distributed in the hope that it will be useful, 0041 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0042 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0043 % GNU Lesser General Public License for more details. 0044 % 0045 % You can obtain a copy of the GNU Lesser General Public License from 0046 % https://www.gnu.org/licenses/ . 0047 % See files gpl-3.0.txt and lgpl-3.0.txt included in this distribution. 0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0049 if nargin<3 0050 m=''; 0051 end 0052 [d,e,r]=v_sort([x(:)']); % find order of x values 0053 [d,e,j]=v_sort([y(:)' x(:)']); 0054 ny=numel(y); 0055 k=j(ny+1:end)-r; % next lower element of y for each x (in range [0,ny]) 0056 i=max(min(k,ny-1),1); % force to lie in range [1,ny-1] 0057 f=(x-y(i))./(y(i+1)-y(i)); % fractional position within the interval 0058 klo=k<1; 0059 if any(klo) 0060 if any(m=='c') 0061 f(klo)=0; 0062 elseif any(m=='n') 0063 i(klo)=NaN; 0064 f(klo)=NaN; 0065 elseif any(m=='z') 0066 i(klo)=0; 0067 end 0068 end 0069 khi=k>=ny; 0070 if any(khi) 0071 if any(m=='C') 0072 f(khi)=1; 0073 elseif any(m=='N') 0074 i(khi)=NaN; 0075 f(khi)=NaN; 0076 elseif any(m=='Z') 0077 i(khi)=ny; 0078 end 0079 end 0080 i=reshape(i,size(x)); % force shape to match x 0081 f=reshape(f,size(x)); % force shape to match x