7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
|
2 ## 2007 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 -*- |
4885
|
21 ## @deftypefn {Function File} {} ranks (@var{x}, @var{dim}) |
8035
|
22 ## Return the ranks of @var{x} along the first non-singleton dimension |
|
23 ## adjust for ties. If the optional argument @var{dim} is |
4885
|
24 ## given, operate along this dimension. |
3453
|
25 ## @end deftypefn |
3426
|
26 |
5428
|
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
28 ## Description: Compute ranks |
3200
|
29 |
4885
|
30 ## This code was rather ugly, since it didn't use sort due to the |
|
31 ## fact of how to deal with ties. Now it does use sort and its |
|
32 ## even uglier!!! At least it handles NDArrays.. |
|
33 |
|
34 function y = ranks (x, dim) |
|
35 |
|
36 if (nargin != 1 && nargin != 2) |
6046
|
37 print_usage (); |
4885
|
38 endif |
3426
|
39 |
4885
|
40 nd = ndims (x); |
|
41 sz = size (x); |
|
42 if (nargin != 2) |
4886
|
43 ## Find the first non-singleton dimension. |
4885
|
44 dim = 1; |
4886
|
45 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
46 dim = dim + 1; |
|
47 endwhile |
|
48 if (dim > nd) |
|
49 dim = 1; |
|
50 endif |
|
51 else |
4886
|
52 if (! (isscalar (dim) && dim == round (dim)) |
|
53 && dim > 0 |
|
54 && dim < (nd + 1)) |
4885
|
55 error ("ranks: dim must be an integer and valid dimension"); |
|
56 endif |
3200
|
57 endif |
3426
|
58 |
4886
|
59 if (sz(dim) == 1) |
4885
|
60 y = ones(sz); |
|
61 else |
|
62 ## The algorithm works only on dim=1, so permute if necesary |
|
63 if (dim != 1) |
|
64 perm = [1 : nd]; |
4886
|
65 perm(1) = dim; |
|
66 perm(dim) = 1; |
4885
|
67 x = permute (x, perm); |
|
68 endif |
4886
|
69 sz = size (x); |
4885
|
70 infvec = -Inf * ones ([1, sz(2 : end)]); |
4942
|
71 [xs, xi] = sort (x); |
4885
|
72 eq_el = find (diff ([xs; infvec]) == 0); |
|
73 if (isempty (eq_el)) |
4942
|
74 [eq_el, y] = sort (xi); |
4885
|
75 else |
|
76 runs = complement (eq_el+1, eq_el); |
|
77 len = diff (find (diff ([Inf; eq_el; -Inf]) != 1)) + 1; |
4942
|
78 [eq_el, y] = sort (xi); |
4885
|
79 for i = 1 : length(runs) |
4942
|
80 y (xi (runs (i) + [0:(len(i)-1)]) + floor (runs (i) ./ sz(1)) |
|
81 * sz(1)) = eq_el(runs(i)) + (len(i) - 1) / 2; |
4885
|
82 endfor |
|
83 endif |
|
84 if (dim != 1) |
|
85 y = permute (y, perm); |
|
86 endif |
3200
|
87 endif |
3426
|
88 |
3200
|
89 endfunction |