Mercurial > hg > octave-nkf
annotate scripts/general/sortrows.m @ 15144:9cc337ced51a
build: Update OCTAVE_UMFPACK_SEPARATE_SPLIT macro to look for SuiteSparse header file.
* acinclude.m4: Update OCTAVE_UMFPACK_SEPARATE_SPLIT macro to look for
SuiteSparse header file.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 10 Aug 2012 12:56:15 -0700 |
parents | 72c96de7a403 |
children | d63878346099 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13293
diff
changeset
|
1 ## Copyright (C) 2000-2012 Daniel Calvelo |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
2 ## Copyright (C) 2009 Jaroslav Hajek |
5178 | 3 ## |
5181 | 4 ## This file is part of Octave. |
5178 | 5 ## |
5181 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5181 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
5178 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5178 | 19 |
5182 | 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 | 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 | 27 ## column is sorted in descending order. |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
28 ## @seealso{sort} |
5182 | 29 ## @end deftypefn |
5178 | 30 |
5181 | 31 ## Author: Daniel Calvelo, Paul Kienzle |
32 ## Adapted-by: jwe | |
5178 | 33 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
34 function [s, i] = sortrows (A, c) |
7678 | 35 |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
36 if (nargin < 1 || nargin > 2) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
37 print_usage (); |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
38 endif |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
39 |
13293
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
40 if (nargin == 2) |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
41 if (! (isnumeric (c) && isvector (c))) |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
42 error ("sortrows: C must be a numeric vector"); |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
43 elseif (any (c == 0) || any (abs (c) > columns (A))) |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
44 error ("sortrows: all elements of C must be in the range [1, columns (A)]"); |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
45 endif |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
46 endif |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
47 |
7678 | 48 default_mode = "ascend"; |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
49 reverse_mode = "descend"; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
50 |
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 if (issparse (A)) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
52 ## FIXME: Eliminate this case once __sort_rows_idx__ is fixed to |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
53 ## handle sparse matrices. |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
54 if (nargin == 1) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
55 i = sort_rows_idx_generic (default_mode, reverse_mode, A); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
56 else |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
57 i = sort_rows_idx_generic (default_mode, reverse_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
|
58 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
59 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
|
60 i = __sort_rows_idx__ (A, default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
61 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
|
62 i = __sort_rows_idx__ (A(:,c), default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
63 elseif (all (c < 0)) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
64 i = __sort_rows_idx__ (A(:,-c), reverse_mode); |
5178 | 65 else |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
66 ## Otherwise, fall back to the old algorithm. |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
67 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c); |
5178 | 68 endif |
69 | |
10850
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
70 ## Only bother to compute s if needed. |
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
71 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
|
72 s = A(i,:); |
10850
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
73 endif |
5181 | 74 |
5178 | 75 endfunction |
7678 | 76 |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
77 function i = sort_rows_idx_generic (default_mode, reverse_mode, m, c) |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
78 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
79 if (nargin == 3) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
80 indices = [1:columns(m)]'; |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
81 mode(1:columns(m)) = {default_mode}; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
82 else |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
83 for j = 1:length (c); |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
84 if (c(j) < 0) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
85 mode{j} = reverse_mode; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
86 else |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
87 mode{j} = default_mode; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
88 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
89 endfor |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
90 indices = abs (c(:)); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
91 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
92 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
93 ## 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
|
94 ## 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
|
95 ## 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
|
96 ## index j. |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
97 indices = flipud (indices); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
98 mode = flipud (mode'); |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
99 i = [1:rows(m)]'; |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
100 for j = 1:length (indices); |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
101 [~, idx] = sort (m(i, indices(j)), mode{j}); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
102 i = i(idx); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
103 endfor |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
104 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
105 endfunction |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
106 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
107 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
108 %!test |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
109 %! 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
|
110 %! c = [1, -2]; |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
111 %! [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
|
112 %! [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
|
113 %! 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
|
114 %! 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
|
115 %! 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
|
116 %! 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
|
117 %! 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
|
118 |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
119 %!test |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
120 %! 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
|
121 %! c = 1; |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
122 %! [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
|
123 %! [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
|
124 %! 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
|
125 %! 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
|
126 %! 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
|
127 %! 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
|
128 %! assert (idx, sidx); |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
129 |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
130 %% Test input validation |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
131 %!error sortrows () |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
132 %!error sortrows (1, 2, 3) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
133 %!error sortrows (1, "ascend") |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
134 %!error sortrows (1, ones (2,2)) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
135 %!error sortrows (1, 0) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
136 %!error sortrows (1, 2) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
137 |