1 ## Copyright (C) 2000-2012 Daniel Calvelo
2 ## Copyright (C) 2009 Jaroslav Hajek
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {[@var{s}, @var{i}] =} sortrows (@var{A})
22 ## @deftypefnx {Function File} {[@var{s}, @var{i}] =} sortrows (@var{A}, @var{c})
23 ## Sort the rows of the matrix @var{A} according to the order of the
24 ## columns specified in @var{c}. If @var{c} is omitted, a
25 ## lexicographical sort is used. By default ascending order is used
26 ## however if elements of @var{c} are negative then the corresponding
27 ## column is sorted in descending order.
31 ## Author: Daniel Calvelo, Paul Kienzle
34 function [s, i] = sortrows (A, c)
36 if (nargin < 1 || nargin > 2)
41 if (! (isnumeric (c) && isvector (c)))
42 error ("sortrows: C must be a numeric vector");
43 elseif (any (c == 0) || any (abs (c) > columns (A)))
44 error ("sortrows: all elements of C must be in the range [1, columns (A)]");
48 default_mode = "ascend";
49 reverse_mode = "descend";
52 ## FIXME: Eliminate this case once __sort_rows_idx__ is fixed to
53 ## handle sparse matrices.
55 i = sort_rows_idx_generic (default_mode, reverse_mode, A);
57 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c);
60 i = __sort_rows_idx__ (A, default_mode);
62 i = __sort_rows_idx__ (A(:,c), default_mode);
64 i = __sort_rows_idx__ (A(:,-c), reverse_mode);
66 ## Otherwise, fall back to the old algorithm.
67 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c);
70 ## Only bother to compute s if needed.
77 function i = sort_rows_idx_generic (default_mode, reverse_mode, m, c)
80 indices = [1:columns(m)]';
81 mode(1:columns(m)) = {default_mode};
85 mode{j} = reverse_mode;
87 mode{j} = default_mode;
93 ## Since sort is 'stable' the order of identical elements will be
94 ## preserved, so by traversing the sort indices in reverse order we
95 ## will make sure that identical elements in index i are subsorted by
97 indices = flipud (indices);
98 mode = flipud (mode');
100 for j = 1:length (indices);
101 [~, idx] = sort (m(i, indices(j)), mode{j});
109 %! m = [1, 1; 1, 2; 3, 6; 2, 7];
111 %! [x, idx] = sortrows (m, c);
112 %! [sx, sidx] = sortrows (sparse (m), c);
113 %! assert (x, [1, 2; 1, 1; 2, 7; 3, 6]);
114 %! assert (idx, [2; 1; 4; 3]);
115 %! assert (issparse (sx));
116 %! assert (x, full (sx));
117 %! assert (idx, sidx);
122 %! [x, idx] = sortrows (m, c);
123 %! [sx, sidx] = sortrows (sparse (m), c);
126 %! assert (issparse (sx));
127 %! assert (x, full (sx));
128 %! assert (idx, sidx);
130 %% Test input validation
132 %!error sortrows (1, 2, 3)
133 %!error sortrows (1, "ascend")
134 %!error sortrows (1, ones (2,2))
135 %!error sortrows (1, 0)
136 %!error sortrows (1, 2)