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