]> Creatis software - CreaPhase.git/blob - octave_packages/m/deprecated/cut.m
update packages
[CreaPhase.git] / octave_packages / m / deprecated / cut.m
1 ## Copyright (C) 1996-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} cut (@var{x}, @var{breaks})
21 ## Create categorical data from numerical or continuous data by
22 ## cutting into intervals.
23 ##
24 ## If @var{breaks} is a scalar, the data is cut into that many
25 ## equal-width intervals.  If @var{breaks} is a vector of break points,
26 ## the category has @code{length (@var{breaks}) - 1} groups.
27 ##
28 ## The returned value is a vector of the same size as @var{x} telling
29 ## which group each point in @var{x} belongs to.  Groups are labelled
30 ## from 1 to the number of groups; points outside the range of
31 ## @var{breaks} are labelled by @code{NaN}.
32 ## @seealso{histc}
33 ## @end deftypefn
34
35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
36 ## Description: Cut data into intervals
37
38 function group = cut (x, breaks)
39
40   persistent warned = false;
41   if (! warned)
42     warned = true;
43     warning ("Octave:deprecated-function",
44              "cut is obsolete and will be removed from a future version of Octave; please use histc instead");
45   endif
46
47   if (nargin != 2)
48     print_usage ();
49   endif
50
51   if (!isvector (x))
52     error ("cut: X must be a vector");
53   endif
54   if isscalar (breaks)
55     breaks = linspace (min (x), max (x), breaks + 1);
56     breaks(1) = breaks(1) - 1;
57   elseif isvector (breaks)
58     breaks = sort (breaks);
59   else
60     error ("cut: BREAKS must be a scalar or vector");
61   endif
62
63   group = NaN (size (x));
64   m = length (breaks);
65   if any (k = find ((x >= min (breaks)) & (x < max (breaks))))
66     n = length (k);
67     group(k) = sum ((ones (m, 1) * reshape (x(k), 1, n))
68                     >= (reshape (breaks, m, 1) * ones (1, n)));
69   endif
70
71 endfunction