annotate scripts/general/sortrows.m @ 8325:b93ac0586e4b

spelling corrections Here is a patch with some spelling corrections to the manual. changeset: 8308:aeaf884ea9af user: Brian Gough <bjg@gnu.org> date: Fri Nov 07 09:26:17 2008 -0500 summary: [docs] assoicated => associated
author Brian Gough<bjg@network-theory.co.uk>
date Mon, 17 Nov 2008 11:38:39 +0100
parents 5988e08c1ae6
children fd11a08a9b31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2000, 2005, 2007 Daniel Calvelo
5178
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
8 ## your option) any later version.
5181
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
18
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
19 ## -*- texinfo -*-
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
20 ## @deftypefn {Function File} {} sortrows (@var{a}, @var{c})
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
21 ## 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
22 ## columns specified in @var{c}. If @var{c} is omitted, a
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
23 ## lexicographical sort is used. By default ascending order is used
8325
b93ac0586e4b spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents: 7678
diff changeset
24 ## however if elements of @var{c} are negative then the corresponding
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
25 ## column is sorted in descending order.
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
26 ## @end deftypefn
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
27
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
28 ## Author: Daniel Calvelo, Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
29 ## Adapted-by: jwe
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
30
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
31 function [s, i] = sortrows (m, c)
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
32
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
33 default_mode = "ascend";
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
34 other_mode = "descend";
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
35 if (nargin < 2)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
36 indices = [1:size(m,2)]';
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
37 mode{1:size(m,2)} = default_mode;
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
38 else
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
39 for ii = 1:length (c);
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
40 if (c(ii) < 0)
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
41 mode{ii} = other_mode;
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
42 else
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
43 mode{ii} = default_mode;
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
44 endif
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
45 endfor
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
46 indices = abs(c(:));
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
47 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
48
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
49 if (ischar (m))
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
50 s = toascii (m);
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
51 else
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
52 s = m;
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
53 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
54
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
55 ## 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
56 ## 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
57 ## 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
58 ## index j.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
59 indices = flipud (indices);
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
60 mode = flipud (mode');
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
61 i = [1:size(m,1)]';
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
62 for ii = 1:length (indices);
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
63 [trash, idx] = sort (s(:,indices(ii)), mode{ii});
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
64 s = s(idx,:);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
65 i = i(idx);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
66 endfor
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
67
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
68 if (ischar (m))
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
69 s = char (s);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
70 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
71
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
72 endfunction
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
73
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
74 %!shared x, idx
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
75 %! [x, idx] = sortrows ([1, 1; 1, 2; 3, 6; 2, 7], [1, -2]);
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
76 %!assert (x, [1, 2; 1, 1; 2, 7; 3, 6]);
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
77 %!assert (idx, [2; 1; 4; 3]);