7017
|
1 ## Copyright (C) 2000, 2005, 2007 Daniel Calvelo |
5178
|
2 ## |
5181
|
3 ## This file is part of Octave. |
5178
|
4 ## |
5181
|
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. |
5181
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
5178
|
14 ## |
|
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/>. |
5178
|
18 |
5182
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} sortrows (@var{a}, @var{c}) |
|
21 ## Sort the rows of the matrix @var{a} according to the order of the |
|
22 ## columns specified in @var{c}. If @var{c} is omitted, a |
|
23 ## lexicographical sort is used. |
|
24 ## @end deftypefn |
5178
|
25 |
5181
|
26 ## Author: Daniel Calvelo, Paul Kienzle |
|
27 ## Adapted-by: jwe |
5178
|
28 |
|
29 function [s, i] = sortrows (m, c) |
|
30 |
5181
|
31 if (nargin < 2) |
|
32 indices = [1:size(m,2)]'; |
5178
|
33 else |
5181
|
34 indices = c(:); |
5178
|
35 endif |
|
36 |
5443
|
37 if (ischar (m)) |
5178
|
38 s = toascii (m); |
|
39 else |
|
40 s = m; |
|
41 endif |
|
42 |
5181
|
43 ## Since sort is 'stable' the order of identical elements will be |
|
44 ## preserved, so by traversing the sort indices in reverse order we |
|
45 ## will make sure that identical elements in index i are subsorted by |
|
46 ## index j. |
5178
|
47 indices = flipud (indices); |
5181
|
48 i = [1:size(m,1)]'; |
|
49 for ii = 1:length (indices); |
|
50 [trash, idx] = sort (s(:,indices(ii))); |
|
51 s = s(idx,:); |
|
52 i = i(idx); |
5178
|
53 endfor |
5181
|
54 |
5443
|
55 if (ischar (m)) |
|
56 s = char (s); |
5178
|
57 endif |
5181
|
58 |
5178
|
59 endfunction |