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