Mercurial > hg > octave-lyh
annotate scripts/general/sortrows.m @ 10412:d2ac9433cd09
append AM_CPPFLAGS to libcruft_la_CPPFLAGS
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 15 Mar 2010 16:56:14 -0400 |
parents | 63249224f78d |
children | 95c3e38098bf |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2007, 2008 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 -*- |
21 ## @deftypefn {Function File} {} sortrows (@var{a}, @var{c}) | |
22 ## Sort the rows of the matrix @var{a} according to the order of the | |
23 ## columns specified in @var{c}. If @var{c} is omitted, a | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## lexicographical sort is used. By default ascending order is used |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7678
diff
changeset
|
25 ## however if elements of @var{c} are negative then the corresponding |
7678 | 26 ## column is sorted in descending order. |
5182 | 27 ## @end deftypefn |
5178 | 28 |
5181 | 29 ## Author: Daniel Calvelo, Paul Kienzle |
30 ## Adapted-by: jwe | |
5178 | 31 |
32 function [s, i] = sortrows (m, c) | |
7678 | 33 |
34 default_mode = "ascend"; | |
35 other_mode = "descend"; | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
36 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
37 if (issparse (m)) |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
38 ## 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
|
39 ## handle sparse matrices. |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
40 if (nargin == 1) |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
41 i = sort_rows_idx_generic (default_mode, other_mode, m); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
42 else |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
43 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
|
44 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
45 elseif (nargin == 1) |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
46 i = __sort_rows_idx__ (m, default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
47 elseif (all (c > 0)) |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
48 i = __sort_rows_idx__ (m(:,c), default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
49 elseif (all (c < 0)) |
8764 | 50 i = __sort_rows_idx__ (m(:,-c), other_mode); |
5178 | 51 else |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
52 ## Otherwise, fall back to the old algorithm. |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
53 i = sort_rows_idx_generic (default_mode, other_mode, m, c); |
5178 | 54 endif |
55 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
56 s = m(i,:); |
5181 | 57 |
5178 | 58 endfunction |
7678 | 59 |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
60 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
|
61 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
62 if (nargin == 3) |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
63 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
|
64 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
|
65 else |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
66 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
|
67 if (c(ii) < 0) |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
68 mode{ii} = other_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 mode{ii} = default_mode; |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
71 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
72 endfor |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
73 indices = abs(c(:)); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
74 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
75 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
76 ## 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
|
77 ## 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
|
78 ## 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
|
79 ## index j. |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
80 indices = flipud (indices); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
81 mode = flipud (mode'); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
82 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
|
83 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
|
84 [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
|
85 i = i(idx); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
86 endfor |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
87 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
88 endfunction |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
89 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
90 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
91 %!shared m, c, x, idx, sx, sidx |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
92 %! 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
|
93 %! c = [1, -2]; |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
94 %! [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
|
95 %! [sx, sidx] = sortrows (sparse (m), c); |
7678 | 96 %!assert (x, [1, 2; 1, 1; 2, 7; 3, 6]); |
97 %!assert (idx, [2; 1; 4; 3]); | |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
98 %!assert (issparse (sx)); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
99 %!assert (x, full (sx)); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
100 %!assert (idx, sidx); |