3200
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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) |
|
41 usage ("cut (X, BREAKS)"); |
|
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 |