Mercurial > hg > octave-lyh
annotate scripts/general/sortrows.m @ 8721:e9cb742df9eb
imported patch sort3.diff
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 11 Feb 2009 15:25:53 +0100 |
parents | fd11a08a9b31 |
children | 3ef774603887 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2005, 2007 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 | |
7678 | 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)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
38 error ("sortrows: sparse matrices not yet supported"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
39 endif |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
40 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
41 ## If the sort is homogeneous, we use the built-in faster algorithm. |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
42 if (nargin == 1) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
43 i = __sortrows_idx__ (m, default_mode); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
44 elseif (all (c > 0)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
45 i = __sortrows_idx__ (m(:,c), default_mode); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
46 elseif (all (c < 0)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
47 i = __sortrows_idx__ (m(:,c), other_mode); |
5178 | 48 else |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
49 ## Otherwise, fall back to the old algorithm |
7678 | 50 for ii = 1:length (c); |
51 if (c(ii) < 0) | |
52 mode{ii} = other_mode; | |
53 else | |
54 mode{ii} = default_mode; | |
55 endif | |
56 endfor | |
57 indices = abs(c(:)); | |
5178 | 58 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
59 ## Since sort is 'stable' the order of identical elements will be |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
60 ## preserved, so by traversing the sort indices in reverse order we |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
61 ## will make sure that identical elements in index i are subsorted by |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
62 ## index j. |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
63 indices = flipud (indices); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
64 mode = flipud (mode'); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
65 i = [1:size(m,1)]'; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
66 for ii = 1:length (indices); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
67 [trash, idx] = sort (m(i, indices(ii)), mode{ii}); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
68 i = i(idx); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
69 endfor |
5178 | 70 endif |
71 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
72 s = m(i,:); |
5181 | 73 |
5178 | 74 endfunction |
7678 | 75 |
76 %!shared x, idx | |
77 %! [x, idx] = sortrows ([1, 1; 1, 2; 3, 6; 2, 7], [1, -2]); | |
78 %!assert (x, [1, 2; 1, 1; 2, 7; 3, 6]); | |
79 %!assert (idx, [2; 1; 4; 3]); |