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 |
|
17 ## usage: cut (X, BREAKS) |
|
18 ## |
|
19 ## Create categorical data out of numerical or continuous data by |
|
20 ## cutting into intervals. |
|
21 ## |
|
22 ## If BREAKS is a scalar, the data is cut into that many equal-width |
|
23 ## intervals. If BREAKS is a vector of break points, the category has |
|
24 ## length(BREAKS)-1 groups. |
|
25 ## |
|
26 ## Returns a vector of the same size as X telling which group each point |
|
27 ## in X belongs to. Groups are labelled from 1 to the number of groups; |
|
28 ## points outside the range of BREAKS are labelled by NaN. |
|
29 |
|
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
31 ## Description: Cut data into intervals |
|
32 |
|
33 function group = cut (X, BREAKS) |
3426
|
34 |
3200
|
35 if (nargin != 2) |
|
36 usage ("cut (X, BREAKS)"); |
|
37 endif |
|
38 |
|
39 if !is_vector (X) |
|
40 error ("cut: X must be a vector"); |
|
41 endif |
|
42 if is_scalar (BREAKS) |
|
43 BREAKS = linspace (min (X), max (X), BREAKS + 1); |
|
44 BREAKS(1) = BREAKS(1) - 1; |
|
45 elseif is_vector (BREAKS) |
|
46 BREAKS = sort (BREAKS); |
|
47 else |
|
48 error ("cut: BREAKS must be a scalar or vector"); |
|
49 endif |
|
50 |
|
51 group = NaN * ones (size (X)); |
|
52 m = length (BREAKS); |
|
53 if any (k = find ((X >= min (BREAKS)) & (X <= max (BREAKS)))) |
|
54 n = length (k); |
|
55 group(k) = sum ((ones (m, 1) * reshape (X(k), 1, n)) |
3426
|
56 > (reshape (BREAKS, m, 1) * ones (1, n))); |
3200
|
57 endif |
|
58 |
|
59 endfunction |