Mercurial > octave-nkf
annotate scripts/statistics/base/ranks.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | d0b799dafede |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) 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 |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3453 | 19 ## -*- texinfo -*- |
4885 | 20 ## @deftypefn {Function File} {} ranks (@var{x}, @var{dim}) |
8035 | 21 ## Return the ranks of @var{x} along the first non-singleton dimension |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10669
diff
changeset
|
22 ## adjusted for ties. If the optional argument @var{dim} is |
4885 | 23 ## given, operate along this dimension. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
24 ## @seealso{spearman,kendall} |
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 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
40 if (!isnumeric(x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
41 error ("ranks: X must be a numeric vector or matrix"); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
42 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
43 |
4885 | 44 nd = ndims (x); |
45 sz = size (x); | |
46 if (nargin != 2) | |
4886 | 47 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
48 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
49 if (isempty (dim)) |
4885 | 50 dim = 1; |
51 endif | |
52 else | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
53 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10687
diff
changeset
|
54 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
55 error ("ranks: DIM must be an integer and a valid dimension"); |
4885 | 56 endif |
3200 | 57 endif |
3426 | 58 |
4886 | 59 if (sz(dim) == 1) |
4885 | 60 y = ones(sz); |
61 else | |
8507 | 62 ## The algorithm works only on dim = 1, so permute if necesary. |
4885 | 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); |
10540
952d4df5b686
Eliminate NaN*ones and Inf*ones constructs and just use Nan() and Inf()
Rik <code@nomad.inbox5.com>
parents:
9051
diff
changeset
|
70 infvec = -Inf ([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 |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
76 runs = setdiff (eq_el, eq_el+1); |
4885 | 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) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
80 y (xi (runs (i) + [0:(len(i)-1)]) + floor (runs (i) ./ sz(1)) |
10549 | 81 * sz(1)) = eq_el(runs(i)) + (len(i) - 1) / 2; |
4885 | 82 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 endif |
4885 | 84 if (dim != 1) |
85 y = permute (y, perm); | |
86 endif | |
3200 | 87 endif |
3426 | 88 |
3200 | 89 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
90 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
91 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
92 %!assert(ranks (1:2:10), 1:5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
93 %!assert(ranks (10:-2:1), 5:-1:1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
94 %!assert(ranks ([2, 1, 2, 4]), [2.5, 1, 2.5, 4]) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
95 %!assert(ranks (ones(1, 5)), 3*ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
96 %!assert(ranks (1e6*ones(1, 5)), 3*ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 %!assert(ranks (rand (1, 5), 1), ones(1, 5)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
98 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
99 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
100 %!error ranks () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
101 %!error ranks (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
102 %!error ranks ({1, 2}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
103 %!error ranks (true(2,1)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
104 %!error ranks (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
105 %!error ranks (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
106 %!error ranks (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
107 |