]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/datefind.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / datefind.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {indices =} datefind (subset, superset, tol)
18 ##
19 ## Find any instances of the @code{subset} in the @code{superset} with
20 ## the @code{tol}erance.  @code{tol} is 0 by default.
21 ##
22 ## @seealso{date, datenum}
23 ## @end deftypefn
24
25 function idx = datefind (subset, superset, tol=0)
26
27   if (nargin < 2 || nargin > 3)
28     print_usage ();
29   elseif ! isscalar (tol)
30     error ("datefind: tol must be a scalar")
31   endif
32
33   idx = [];
34   for i = 1:numel (superset)
35     if any (subset(:) - tol <= superset(i) & superset(i) <= subset(:) + tol)
36       idx(end+1, 1) = i;
37     endif
38   endfor
39
40 endfunction
41
42 ## Tests
43 %!assert (datefind (datenum (1999, 7, [10;20]), datenum (1999, 7, 1:31)), [10;20])
44 %!assert (datefind (datenum (1999, 7, [10;20]), datenum (1999, 7, 1:31), 1), [9;10;11;19;20;21])