1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006,
2## 2007, 2008, 2009 Kurt Hornik
4## This file is part of Octave.
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {} ranks (@var{x}, @var{dim})
22## Return the ranks of @var{x} along the first non-singleton dimension
23## adjust for ties. If the optional argument @var{dim} is
24## given, operate along this dimension.
27## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28## Description: Compute ranks
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..
34function y = ranks (x, dim)
36 if (nargin != 1 && nargin != 2)
43 ## Find the first non-singleton dimension.
45 while (dim < nd + 1 && sz(dim) == 1)
52 if (! (isscalar (dim) && dim == round (dim))
55 error ("ranks: dim must be an integer and valid dimension");
62 ## The algorithm works only on dim = 1, so permute if necesary.
67 x = permute (x, perm);
70 infvec = -Inf * ones ([1, sz(2 : end)]);
72 eq_el = find (diff ([xs; infvec]) == 0);
74 [eq_el, y] = sort (xi);
76 runs = complement (eq_el+1, eq_el);
77 len = diff (find (diff ([Inf; eq_el; -Inf]) != 1)) + 1;
78 [eq_el, y] = sort (xi);
79 for i = 1 : length(runs)
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;
85 y = permute (y, perm);