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