5178
|
1 ## Copyright (C) 2000 Daniel Calvelo |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
5181
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
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 |
|
24 ## lexicographical sort is used. |
|
25 ## @end deftypefn |
5178
|
26 |
5181
|
27 ## Author: Daniel Calvelo, Paul Kienzle |
|
28 ## Adapted-by: jwe |
5178
|
29 |
|
30 function [s, i] = sortrows (m, c) |
|
31 |
5181
|
32 if (nargin < 2) |
|
33 indices = [1:size(m,2)]'; |
5178
|
34 else |
5181
|
35 indices = c(:); |
5178
|
36 endif |
|
37 |
5181
|
38 if (isstr (m)) |
5178
|
39 s = toascii (m); |
|
40 else |
|
41 s = m; |
|
42 endif |
|
43 |
5181
|
44 ## Since sort is 'stable' the order of identical elements will be |
|
45 ## preserved, so by traversing the sort indices in reverse order we |
|
46 ## will make sure that identical elements in index i are subsorted by |
|
47 ## index j. |
5178
|
48 indices = flipud (indices); |
5181
|
49 i = [1:size(m,1)]'; |
|
50 for ii = 1:length (indices); |
|
51 [trash, idx] = sort (s(:,indices(ii))); |
|
52 s = s(idx,:); |
|
53 i = i(idx); |
5178
|
54 endfor |
5181
|
55 |
5178
|
56 if (isstr (m)) |
5181
|
57 s = setstr (s); |
5178
|
58 endif |
5181
|
59 |
5178
|
60 endfunction |