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 |
|
20 ## B = sortrows (A) |
|
21 ## returns matrix with rows sorted "lexicographically" |
|
22 ## |
|
23 ## B = sortrows(A, c) |
|
24 ## returns matrix with rows sorted according to the order of the |
|
25 ## columns specified in c. |
|
26 ## |
|
27 ## Set implicit_str_to_num_ok and implicit_num_to_str_ok to 1 if you |
|
28 ## use this for string sorting and octave 2.1.50 or earlier. |
|
29 |
5181
|
30 ## Author: Daniel Calvelo, Paul Kienzle |
|
31 ## Adapted-by: jwe |
5178
|
32 |
|
33 function [s, i] = sortrows (m, c) |
|
34 |
5181
|
35 if (nargin < 2) |
|
36 indices = [1:size(m,2)]'; |
5178
|
37 else |
5181
|
38 indices = c(:); |
5178
|
39 endif |
|
40 |
5181
|
41 if (isstr (m)) |
5178
|
42 s = toascii (m); |
|
43 else |
|
44 s = m; |
|
45 endif |
|
46 |
5181
|
47 ## Since sort is 'stable' the order of identical elements will be |
|
48 ## preserved, so by traversing the sort indices in reverse order we |
|
49 ## will make sure that identical elements in index i are subsorted by |
|
50 ## index j. |
5178
|
51 indices = flipud (indices); |
5181
|
52 i = [1:size(m,1)]'; |
|
53 for ii = 1:length (indices); |
|
54 [trash, idx] = sort (s(:,indices(ii))); |
|
55 s = s(idx,:); |
|
56 i = i(idx); |
5178
|
57 endfor |
5181
|
58 |
5178
|
59 if (isstr (m)) |
5181
|
60 s = setstr (s); |
5178
|
61 endif |
5181
|
62 |
5178
|
63 endfunction |