Home > utility > findInRange.m

findInRange

PURPOSE ^

findInRange: Find the range of a given value

SYNOPSIS ^

function output=findInRange(range, value)

DESCRIPTION ^

 findInRange: Find the range of a given value
    Usage: output=findInRange(range, value);
        range: a sorted monotonically increasing vector
        value: value whose range is to be found
        output: output=0 if value<range[1];
            output=length(range) if range[end]<=value
            output=i if range[i]<=value<range[i+1]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function output=findInRange(range, value)
0002 % findInRange: Find the range of a given value
0003 %    Usage: output=findInRange(range, value);
0004 %        range: a sorted monotonically increasing vector
0005 %        value: value whose range is to be found
0006 %        output: output=0 if value<range[1];
0007 %            output=length(range) if range[end]<=value
0008 %            output=i if range[i]<=value<range[i+1]
0009 
0010 %    Roger Jang, 20041229
0011 
0012 if nargin<1; selfdemo; return; end
0013 
0014 output=0*value;
0015 
0016 for i=1:length(value)
0017     index=find(range-value(i)>0);
0018     if isempty(index)
0019         output(i)=length(range);
0020     else
0021         output(i)=index(1)-1;
0022     end
0023 end
0024 
0025 % ====== Self demo
0026 function selfdemo
0027 range=[2 4 6 8]; value=1;
0028 output=feval(mfilename, range, value);
0029 fprintf('findInRange(%s, %s) = %s\n', mat2str(range), mat2str(value), mat2str(output));
0030 range=[2 4 6 8]; value=2;
0031 output=feval(mfilename, range, value);
0032 fprintf('findInRange(%s, %s) = %s\n', mat2str(range), mat2str(value), mat2str(output));
0033 range=[2 4 6 8]; value=3;
0034 output=feval(mfilename, range, value);
0035 fprintf('findInRange(%s, %s) = %s\n', mat2str(range), mat2str(value), mat2str(output));
0036 range=[2 4 6 8]; value=9;
0037 output=feval(mfilename, range, value);
0038 fprintf('findInRange(%s, %s) = %s\n', mat2str(range), mat2str(value), mat2str(output));
0039 range=[2 4 6 8]; value=[2 3 9];
0040 output=feval(mfilename, range, value);
0041 fprintf('findInRange(%s, %s) = %s\n', mat2str(range), mat2str(value), mat2str(output));
0042

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003