1 ## Copyright (C) 1996-2012 Kurt Hornik
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} cut (@var{x}, @var{breaks})
21 ## Create categorical data from numerical or continuous data by
22 ## cutting into intervals.
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.
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}.
35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
36 ## Description: Cut data into intervals
38 function group = cut (x, breaks)
40 persistent warned = false;
43 warning ("Octave:deprecated-function",
44 "cut is obsolete and will be removed from a future version of Octave; please use histc instead");
52 error ("cut: X must be a vector");
55 breaks = linspace (min (x), max (x), breaks + 1);
56 breaks(1) = breaks(1) - 1;
57 elseif isvector (breaks)
58 breaks = sort (breaks);
60 error ("cut: BREAKS must be a scalar or vector");
63 group = NaN (size (x));
65 if any (k = find ((x >= min (breaks)) & (x < max (breaks))))
67 group(k) = sum ((ones (m, 1) * reshape (x(k), 1, n))
68 >= (reshape (breaks, m, 1) * ones (1, n)));