annotate scripts/general/sortrows.m @ 5443:ec8c33dcd1bf

[project @ 2005-09-08 01:40:57 by jwe]
author jwe
date Thu, 08 Sep 2005 01:40:58 +0000
parents 4c8a2e4e0717
children 93c65f2a5668
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2000 Daniel Calvelo
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
2 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
3 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
4 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
6 ## under the terms of the GNU General Public License as published by
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
8 ## any later version.
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
9 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
14 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5182
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5182
diff changeset
18 ## 02110-1301, USA.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
19
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
20 ## -*- texinfo -*-
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
21 ## @deftypefn {Function File} {} sortrows (@var{a}, @var{c})
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
22 ## Sort the rows of the matrix @var{a} according to the order of the
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
23 ## columns specified in @var{c}. If @var{c} is omitted, a
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
24 ## lexicographical sort is used.
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
25 ## @end deftypefn
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
26
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
27 ## Author: Daniel Calvelo, Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
28 ## Adapted-by: jwe
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
29
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
30 function [s, i] = sortrows (m, c)
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
31
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
32 if (nargin < 2)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
33 indices = [1:size(m,2)]';
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
34 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
35 indices = c(:);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
36 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
37
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
38 if (ischar (m))
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
39 s = toascii (m);
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
40 else
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
41 s = m;
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
42 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
43
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
44 ## Since sort is 'stable' the order of identical elements will be
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
45 ## preserved, so by traversing the sort indices in reverse order we
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
46 ## will make sure that identical elements in index i are subsorted by
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
47 ## index j.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
48 indices = flipud (indices);
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
49 i = [1:size(m,1)]';
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
50 for ii = 1:length (indices);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
51 [trash, idx] = sort (s(:,indices(ii)));
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
52 s = s(idx,:);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
53 i = i(idx);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
54 endfor
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
55
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
56 if (ischar (m))
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
57 s = char (s);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
58 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
59
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
60 endfunction