annotate scripts/general/sortrows.m @ 13172:25467719730b stable

doc: Quiet Texinfo warning in accumarray.m * accumarray.m: Use @pxref for cross-reference inside parentheses
author Rik <octave@nomad.inbox5.com>
date Tue, 20 Sep 2011 12:25:02 -0700
parents e38fb1910563
children 6cebb0c36b1d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11469
diff changeset
1 ## Copyright (C) 2000-2011 Daniel Calvelo
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
2 ## Copyright (C) 2009 Jaroslav Hajek
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
3 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
4 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
5 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
9 ## your option) any later version.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
10 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
14 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
15 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
16 ## 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: 5443
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
19
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
20 ## -*- texinfo -*-
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
21 ## @deftypefn {Function File} {[@var{s}, @var{i}] =} sortrows (@var{A})
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
22 ## @deftypefnx {Function File} {[@var{s}, @var{i}] =} sortrows (@var{A}, @var{c})
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
23 ## Sort the rows of the matrix @var{A} according to the order of the
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
24 ## columns specified in @var{c}. If @var{c} is omitted, a
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
25 ## lexicographical sort is used. By default ascending order is used
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
26 ## however if elements of @var{c} are negative then the corresponding
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
27 ## column is sorted in descending order.
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
28 ## @end deftypefn
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
29
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30 ## Author: Daniel Calvelo, Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
31 ## Adapted-by: jwe
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
32
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
33 function [s, i] = sortrows (A, c)
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
34
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
35 default_mode = "ascend";
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
36 other_mode = "descend";
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
37
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
38 if (issparse (A))
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
39 ## FIXME -- eliminate this case once __sort_rows_idx__ is fixed to
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
40 ## handle sparse matrices.
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
41 if (nargin == 1)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
42 i = sort_rows_idx_generic (default_mode, other_mode, A);
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
43 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
44 i = sort_rows_idx_generic (default_mode, other_mode, A, c);
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
45 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
46 elseif (nargin == 1)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
47 i = __sort_rows_idx__ (A, default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
48 elseif (all (c > 0))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
49 i = __sort_rows_idx__ (A(:,c), default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
50 elseif (all (c < 0))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
51 i = __sort_rows_idx__ (A(:,-c), other_mode);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
52 else
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
53 ## Otherwise, fall back to the old algorithm.
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
54 i = sort_rows_idx_generic (default_mode, other_mode, A, c);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
55 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
56
10850
6c57bd7d0808 isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
57 ## Only bother to compute s if needed.
6c57bd7d0808 isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
58 if (isargout (1))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
59 s = A(i,:);
10850
6c57bd7d0808 isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
60 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
61
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
62 endfunction
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
63
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
64 function i = sort_rows_idx_generic (default_mode, other_mode, m, c)
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
65
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
66 if (nargin == 3)
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
67 indices = [1:size(m,2)]';
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
68 mode(1:size(m,2)) = {default_mode};
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
69 else
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
70 for ii = 1:length (c);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
71 if (c(ii) < 0)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9669
diff changeset
72 mode{ii} = other_mode;
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
73 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9669
diff changeset
74 mode{ii} = default_mode;
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
75 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
76 endfor
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
77 indices = abs(c(:));
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
78 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
79
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
80 ## Since sort is 'stable' the order of identical elements will be
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
81 ## preserved, so by traversing the sort indices in reverse order we
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
82 ## will make sure that identical elements in index i are subsorted by
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
83 ## index j.
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
84 indices = flipud (indices);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
85 mode = flipud (mode');
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
86 i = [1:size(m,1)]';
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
87 for ii = 1:length (indices);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
88 [trash, idx] = sort (m(i, indices(ii)), mode{ii});
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
89 i = i(idx);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
90 endfor
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
91
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
92 endfunction
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
93
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
94
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
95 %!test
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
96 %! m = [1, 1; 1, 2; 3, 6; 2, 7];
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
97 %! c = [1, -2];
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
98 %! [x, idx] = sortrows (m, c);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
99 %! [sx, sidx] = sortrows (sparse (m), c);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
100 %! assert (x, [1, 2; 1, 1; 2, 7; 3, 6]);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
101 %! assert (idx, [2; 1; 4; 3]);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
102 %! assert (issparse (sx));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
103 %! assert (x, full (sx));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
104 %! assert (idx, sidx);
12647
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
105
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
106 %!test
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
107 %! m = [1, 0, 0, 4];
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
108 %! c = 1;
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
109 %! [x, idx] = sortrows (m, c);
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
110 %! [sx, sidx] = sortrows (sparse (m), c);
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
111 %! assert (x, m);
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
112 %! assert (idx, 1);
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
113 %! assert (issparse (sx));
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
114 %! assert (x, full (sx));
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
115 %! assert (idx, sidx);