Mercurial > octave
annotate scripts/statistics/ranks.m @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | 363fb10055df |
children | 5d3faba0342e |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30379
diff
changeset
|
3 ## Copyright (C) 1995-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
3426 | 7 ## |
3922 | 8 ## This file is part of Octave. |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
3426 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
3200 | 16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
3426 | 19 ## |
3200 | 20 ## You should have received a copy of the GNU General Public License |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
3200 | 25 |
3453 | 26 ## -*- texinfo -*- |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
27 ## @deftypefn {} {} ranks (@var{x}) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
28 ## @deftypefnx {} {} ranks (@var{x}, @var{dim}) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
29 ## @deftypefnx {} {} ranks (@var{x}, @var{dim}, @var{rtype}) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
30 ## Return the ranks (in the sense of order statistics) of @var{x} along the |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
31 ## first non-singleton dimension adjusted for ties. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
32 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
33 ## If the optional @var{dim} argument is given, operate along this dimension. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
34 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
35 ## The optional parameter @var{rtype} determines how ties are handled. All |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
36 ## examples below assume an input of @code{[ 1, 2, 2, 4 ]}. |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
38 ## @table @asis |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
39 ## @item 0 or @qcode{"fractional"} (default) for fractional ranking (1, 2.5, |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
40 ## 2.5, 4); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
41 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
42 ## @item 1 or @qcode{"competition"} for competition ranking (1, 2, 2, 4); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
43 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
44 ## @item 2 or @qcode{"modified"} for modified competition ranking (1, 3, 3, 4); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
45 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
46 ## @item 3 or @qcode{"ordinal"} for ordinal ranking (1, 2, 3, 4); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
47 ## |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
48 ## @item 4 or @qcode{"dense"} for dense ranking (1, 2, 2, 3). |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
49 ## @end table |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
50 ## |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 ## @seealso{spearman, kendall} |
3453 | 52 ## @end deftypefn |
3426 | 53 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
54 function y = ranks (x, dim, rtype = 0) |
3200 | 55 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
56 if (nargin < 1) |
6046 | 57 print_usage (); |
4885 | 58 endif |
3426 | 59 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
61 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
|
62 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
63 |
4885 | 64 nd = ndims (x); |
65 sz = size (x); | |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
66 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
67 if (nargin < 2 || isempty (dim)) |
4886 | 68 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 (dim = find (sz > 1, 1)) || (dim = 1); |
4885 | 70 else |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
71 if (! (isscalar (dim) && dim == fix (dim) && dim > 0)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
72 error ("ranks: DIM must be an integer and a valid dimension"); |
4885 | 73 endif |
3200 | 74 endif |
3426 | 75 |
4886 | 76 if (sz(dim) == 1) |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
77 y = ones (sz); # dimension DIM is singleton, so all are ranked first. |
4885 | 78 else |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
79 ## The algorithm works only on dim = 1, so permute if necessary. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
80 ## FIXME: Most all functions now accept a dim argument. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
81 ## Would it be faster not to permute and use the dim argument |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
82 ## to sort, find, cumsum, etc.? |
4885 | 83 if (dim != 1) |
84 perm = [1 : nd]; | |
4886 | 85 perm(1) = dim; |
86 perm(dim) = 1; | |
4885 | 87 x = permute (x, perm); |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
88 sz = size (x); |
4885 | 89 endif |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
90 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
91 [sx, ids] = sort (x); # sx is sorted x. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
92 lin = repmat ((1:rows (x))', [1, sz(2:end)]); # linearly increasing array. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
93 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
94 switch (rtype) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
95 case {0, "fractional"}; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
96 lin = (_competition (lin, sx, sz) + _modified (lin, sx, sz)) / 2; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
97 case {1, "competition"}; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
98 lin = _competition (lin, sx, sz); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
99 case {2, "modified"}; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
100 lin = _modified (lin, sx, sz); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
101 case {3, "ordinal"}; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
102 ## no processing needed here. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
103 case {4, "dense"}; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
104 lin = _dense (lin, sx, sz); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
105 otherwise |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
106 if (! ischar (rtype)) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
107 rtype = num2str (rtype); |
28947
b73a54c31152
maint: Use Octave-specific end keywords rather than just 'end'.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
108 endif |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
109 error ("ranks: unknown RTYPE '%s'", rtype); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
110 endswitch |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
111 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
112 y = NaN (size (lin)); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
113 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
114 ## Offsets to map indices into each column to indices into the linear array. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
115 ## FIXME: Would sub2ind be faster here? |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
116 idf = zeros (sz); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
117 idf(1, :) = 0 : sz(1) : (numel (ids)-1); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
118 idf(:, :) = repmat (idf(1, :), [sz(1), ones(1,length(sz)-1)]); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
119 y(ids + idf) = lin; |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
120 |
4885 | 121 if (dim != 1) |
122 y = permute (y, perm); | |
123 endif | |
3200 | 124 endif |
3426 | 125 |
3200 | 126 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
127 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
128 function linnew = _dense (lin, sx, sz) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
129 infvec = -Inf ([1, sz(2:end)]); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
130 fnewp = logical (diff ([infvec; sx])); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
131 linnew = cumsum (fnewp, 1); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
132 endfunction |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
133 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
134 function linnew = _competition (lin, sx, sz) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
135 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
136 ## Stop increasing lin when sx does not increase. Otherwise, same as before. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
137 infvec = -Inf ([1, sz(2:end)]); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
138 fnewp = find (diff ([infvec; sx])); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
139 linnew = zeros (size (lin)); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
140 linnew(fnewp) = lin(fnewp); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
141 linnew = cummax (linnew, 1); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
142 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
143 endfunction |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
144 |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
145 function linnew = _modified (lin, sx, sz) |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
146 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
147 ## Traverse lin backwards. Stop decreasing it when sx doesn't decrease. |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
148 infvec = Inf ([1, sz(2:end)]); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
149 fnewp = find (diff ([sx; infvec])); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
150 linnew = Inf (size (lin)); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
151 linnew(fnewp) = lin(fnewp); |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
152 linnew = flip (cummin (flip (linnew, 1)), 1); |
30379
363fb10055df
maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
153 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
154 endfunction |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
155 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
156 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
157 %!assert (ranks (1:2:10), 1:5) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
158 %!assert (ranks (10:-2:1), 5:-1:1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %!assert (ranks ([2, 1, 2, 4]), [2.5, 1, 2.5, 4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %!assert (ranks (ones (1, 5)), 3*ones (1, 5)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
161 %!assert (ranks (1e6*ones (1, 5)), 3*ones (1, 5)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
162 %!assert (ranks (rand (1, 5), 1), ones (1, 5)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
163 |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
164 %!assert (ranks ([1, 2, 2, 4], [], "fractional"), [1, 2.5, 2.5, 4]) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
165 %!assert (ranks ([1, 2, 2, 4], [], "competition"), [1, 2, 2, 4]) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
166 %!assert (ranks ([1, 2, 2, 4], [], "modified"), [1, 3, 3, 4]) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
167 %!assert (ranks ([1, 2, 2, 4], [], "ordinal"), [1, 2, 3, 4]) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
168 %!assert (ranks ([1, 2, 2, 4], [], "dense"), [1, 2, 2, 3]) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
169 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
170 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
171 %!error <Invalid call> ranks () |
26077
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
172 %!error <X must be a numeric vector or matrix> ranks ({1, 2}) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
173 %!error <X must be a numeric vector or matrix> ranks (['A'; 'B']) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
174 %!error <DIM must be an integer> ranks (1, 1.5) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
175 %!error <DIM must be .* a valid dimension> ranks (1, 0) |
31b443b5a6c1
ranks.m: Overhaul function for performance (25X) and addition of tie-breaking (bug #36372).
Dave Goel <deego3@gmail.com>
parents:
25054
diff
changeset
|
176 %!error <unknown RTYPE 'foobar'> ranks (ones (2), 1, "foobar") |