comparison scripts/general/sortrows.m @ 5181:41cd70503c72

[project @ 2005-03-03 05:49:55 by jwe]
author jwe
date Thu, 03 Mar 2005 05:49:55 +0000
parents 6758c11b5b99
children 5b361aa47dff
comparison
equal deleted inserted replaced
5180:e7438487c857 5181:41cd70503c72
1 ## Copyright (C) 2000 Daniel Calvelo 1 ## Copyright (C) 2000 Daniel Calvelo
2 ## 2 ##
3 ## This program is free software; you can redistribute it and/or modify 3 ## This file is part of Octave.
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 4 ##
8 ## This program is distributed in the hope that it will be useful, 5 ## Octave is free software; you can redistribute it and/or modify it
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 6 ## under the terms of the GNU General Public License as published by
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 7 ## the Free Software Foundation; either version 2, or (at your option)
11 ## GNU General Public License for more details. 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.
12 ## 14 ##
13 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software 16 ## along with Octave; see the file COPYING. If not, write to the Free
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA.
16 19
17 ## B = sortrows (A) 20 ## B = sortrows (A)
18 ## returns matrix with rows sorted "lexicographically" 21 ## returns matrix with rows sorted "lexicographically"
19 ## 22 ##
20 ## B = sortrows(A, c) 23 ## B = sortrows(A, c)
22 ## columns specified in c. 25 ## columns specified in c.
23 ## 26 ##
24 ## Set implicit_str_to_num_ok and implicit_num_to_str_ok to 1 if you 27 ## Set implicit_str_to_num_ok and implicit_num_to_str_ok to 1 if you
25 ## use this for string sorting and octave 2.1.50 or earlier. 28 ## use this for string sorting and octave 2.1.50 or earlier.
26 29
27 ## Author: Daniel Calvelo 30 ## Author: Daniel Calvelo, Paul Kienzle
28 ## 2001-02-24 Paul Kienzle 31 ## Adapted-by: jwe
29 ## * cleanup according to Octave conventions
30 ## * return reverse index
31 ## * handle string arguments
32 32
33 function [s, i] = sortrows (m, c) 33 function [s, i] = sortrows (m, c)
34 34
35 if nargin < 2 35 if (nargin < 2)
36 indices = [ 1 : size(m,2) ]'; 36 indices = [1:size(m,2)]';
37 else 37 else
38 indices = c (:); 38 indices = c(:);
39 endif 39 endif
40 40
41 if (isstr (m)) 41 if (isstr (m))
42 s = toascii (m); 42 s = toascii (m);
43 else 43 else
44 s = m; 44 s = m;
45 endif 45 endif
46 46
47 ## since sort is 'stable' the order of identical elements will be 47 ## Since sort is 'stable' the order of identical elements will be
48 ## preserved, so by traversing the sort indices in reverse order 48 ## preserved, so by traversing the sort indices in reverse order we
49 ## we will make sure that identical elements in index i are subsorted 49 ## will make sure that identical elements in index i are subsorted by
50 ## by index j. 50 ## index j.
51 indices = flipud (indices); 51 indices = flipud (indices);
52 i = [1 : size(m,1)]'; 52 i = [1:size(m,1)]';
53 for ii = 1 : length (indices); 53 for ii = 1:length (indices);
54 [ trash, idx ] = sort ( s ( : , indices (ii) ) ); 54 [trash, idx] = sort (s(:,indices(ii)));
55 s = s ( idx, : ); 55 s = s(idx,:);
56 i = i (idx ); 56 i = i(idx);
57 endfor 57 endfor
58
58 if (isstr (m)) 59 if (isstr (m))
59 s = setstr(s); 60 s = setstr (s);
60 endif 61 endif
62
61 endfunction 63 endfunction