Mercurial > hg > octave-lyh
annotate scripts/statistics/base/ranks.m @ 17157:73a3c1580974
Correctly identify PNG image types (bug #32986, bug #36820 and bug #37689)
* __magick_read_.cc (read_images): check original color type from PNG image
attribute because GraphicsMagick changes it for optimization.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Fri, 02 Aug 2013 19:05:42 +0100 |
parents | 5d3a684236b0 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12656
diff
changeset
|
1 ## Copyright (C) 1995-2012 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. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
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 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
40 if (! (isnumeric (x) || islogical (x))) |
11436
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. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
48 (dim = find (sz > 1, 1)) || (dim = 1); |
4885 | 49 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
50 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
|
51 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
52 error ("ranks: DIM must be an integer and a valid dimension"); |
4885 | 53 endif |
3200 | 54 endif |
3426 | 55 |
4886 | 56 if (sz(dim) == 1) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
57 y = ones (sz); |
4885 | 58 else |
8507 | 59 ## The algorithm works only on dim = 1, so permute if necesary. |
4885 | 60 if (dim != 1) |
61 perm = [1 : nd]; | |
4886 | 62 perm(1) = dim; |
63 perm(dim) = 1; | |
4885 | 64 x = permute (x, perm); |
65 endif | |
4886 | 66 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
|
67 infvec = -Inf ([1, sz(2 : end)]); |
4942 | 68 [xs, xi] = sort (x); |
4885 | 69 eq_el = find (diff ([xs; infvec]) == 0); |
70 if (isempty (eq_el)) | |
4942 | 71 [eq_el, y] = sort (xi); |
4885 | 72 else |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
73 runs = setdiff (eq_el, eq_el+1); |
4885 | 74 len = diff (find (diff ([Inf; eq_el; -Inf]) != 1)) + 1; |
4942 | 75 [eq_el, y] = sort (xi); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
76 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
|
77 y (xi (runs (i) + [0:(len(i)-1)]) + floor (runs (i) ./ sz(1)) |
10549 | 78 * sz(1)) = eq_el(runs(i)) + (len(i) - 1) / 2; |
4885 | 79 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
80 endif |
4885 | 81 if (dim != 1) |
82 y = permute (y, perm); | |
83 endif | |
3200 | 84 endif |
3426 | 85 |
3200 | 86 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
87 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
88 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!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
|
90 %!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
|
91 %!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
|
92 %!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
|
93 %!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
|
94 %!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
|
95 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
96 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 %!error ranks () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
98 %!error ranks (1, 2, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
99 %!error ranks ({1, 2}) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
100 %!error ranks (['A'; 'B']) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
101 %!error ranks (1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
102 %!error ranks (1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
103 %!error ranks (1, 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
104 |