annotate scripts/statistics/ranks.m @ 31551:fd29c7a50a78 stable

maint: use commas, semicolons consistently with Octave conventions. * makeValidName.m: Remove %!test and move BIST %!asserts to column 1. * base64decode.m, base64encode.m, which.m, logm.m, uniquetol.m, perms.m: Delete semicolon (';') at end of %!assert BIST. * lin2mu.m, interp2.m, interpn.m, lsqnonneg.m, pqpnonneg.m, uniquetol.m, betainc.m, normalize.m: Add semicolon (';') to end of assert statement within %!test BIST. * __memoize__.m, tar_is_bsd.m, publish.m: Add semicolon (';') to line with keyword "persistent". * stft.m: Use comma (',') after "case" keyword when code immediately follows. * gallery.m: Align commas used in case statements in massive switch block. Remove unnecessary parentheses around a numeric case argument. * ranks.m: Remove semicolon (';') from case statemnt argument.
author Rik <rik@octave.org>
date Sat, 26 Nov 2022 06:32:08 -0800
parents 5d3faba0342e
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
7 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
8 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
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
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
19 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
25
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{y} =} ranks (@var{x})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {@var{y} =} ranks (@var{x}, @var{dim})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
29 ## @deftypefnx {} {@var{y} =} ranks (@var{x}, @var{dim}, @var{rtype})
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
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
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
52 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
57 print_usage ();
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
58 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
64 nd = ndims (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
73 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
74 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
75
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
83 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
84 perm = [1 : nd];
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
85 perm(1) = dim;
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
86 perm(dim) = 1;
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
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)
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30875
diff changeset
95 case {0, "fractional"}
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
96 lin = (_competition (lin, sx, sz) + _modified (lin, sx, sz)) / 2;
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30875
diff changeset
97 case {1, "competition"}
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
98 lin = _competition (lin, sx, sz);
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30875
diff changeset
99 case {2, "modified"}
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
100 lin = _modified (lin, sx, sz);
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30875
diff changeset
101 case {3, "ordinal"}
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
102 ## no processing needed here.
31551
fd29c7a50a78 maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents: 30875
diff changeset
103 case {4, "dense"}
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
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
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
121 if (dim != 1)
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
122 y = permute (y, perm);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 3922
diff changeset
123 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
124 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
125
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
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")