annotate scripts/general/sortrows.m @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents 48d213be5e0e
children 853f96e8008f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8764
diff changeset
1 ## Copyright (C) 2000, 2005, 2007, 2008 Daniel Calvelo
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
2 ## Copyright (C) 2009 Jaroslav Hajek
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
3 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
4 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
5 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
10 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## 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
14 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
15 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
16 ## 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
17 ## 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
18 ## <http://www.gnu.org/licenses/>.
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
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
24 ## 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
25 ## 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
26 ## column is sorted in descending order.
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
27 ## @end deftypefn
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
28
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
29 ## Author: Daniel Calvelo, Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30 ## Adapted-by: jwe
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
31
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
32 function [s, i] = sortrows (m, c)
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
33
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
34 default_mode = "ascend";
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
35 other_mode = "descend";
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
36
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
37 if (issparse (m))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
38 error ("sortrows: sparse matrices not yet supported");
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
39 endif
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
40
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
41 ## If the sort is homogeneous, we use the built-in faster algorithm.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
42 if (nargin == 1)
8733
3ef774603887 rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
43 i = __sort_rows_idx__ (m, default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
44 elseif (all (c > 0))
8733
3ef774603887 rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
45 i = __sort_rows_idx__ (m(:,c), default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
46 elseif (all (c < 0))
8764
48d213be5e0e fix typo in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents: 8733
diff changeset
47 i = __sort_rows_idx__ (m(:,-c), other_mode);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
48 else
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
49 ## Otherwise, fall back to the old algorithm
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
50 for ii = 1:length (c);
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
51 if (c(ii) < 0)
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
52 mode{ii} = other_mode;
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
53 else
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
54 mode{ii} = default_mode;
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
55 endif
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
56 endfor
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
57 indices = abs(c(:));
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
58
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
59 ## Since sort is 'stable' the order of identical elements will be
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
60 ## preserved, so by traversing the sort indices in reverse order we
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
61 ## will make sure that identical elements in index i are subsorted by
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
62 ## index j.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
63 indices = flipud (indices);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
64 mode = flipud (mode');
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
65 i = [1:size(m,1)]';
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
66 for ii = 1:length (indices);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
67 [trash, idx] = sort (m(i, indices(ii)), mode{ii});
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
68 i = i(idx);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
69 endfor
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
70 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
71
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
72 s = m(i,:);
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
73
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
74 endfunction
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
75
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
76 %!shared x, idx
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
77 %! [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
78 %!assert (x, [1, 2; 1, 1; 2, 7; 3, 6]);
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
79 %!assert (idx, [2; 1; 4; 3]);