Mercurial > octave-nkf
diff scripts/general/sortrows.m @ 8721:e9cb742df9eb
imported patch sort3.diff
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 11 Feb 2009 15:25:53 +0100 |
parents | fd11a08a9b31 |
children | 3ef774603887 |
line wrap: on
line diff
--- a/scripts/general/sortrows.m Wed Feb 11 01:48:39 2009 -0500 +++ b/scripts/general/sortrows.m Wed Feb 11 15:25:53 2009 +0100 @@ -1,4 +1,5 @@ ## Copyright (C) 2000, 2005, 2007 Daniel Calvelo +## Copyright (C) 2009 Jaroslav Hajek ## ## This file is part of Octave. ## @@ -32,10 +33,20 @@ default_mode = "ascend"; other_mode = "descend"; - if (nargin < 2) - indices = [1:size(m,2)]'; - mode(1:size(m,2)) = {default_mode}; + + if (issparse (m)) + error ("sortrows: sparse matrices not yet supported"); + endif + + ## If the sort is homogeneous, we use the built-in faster algorithm. + if (nargin == 1) + i = __sortrows_idx__ (m, default_mode); + elseif (all (c > 0)) + i = __sortrows_idx__ (m(:,c), default_mode); + elseif (all (c < 0)) + i = __sortrows_idx__ (m(:,c), other_mode); else + ## Otherwise, fall back to the old algorithm for ii = 1:length (c); if (c(ii) < 0) mode{ii} = other_mode; @@ -44,30 +55,21 @@ endif endfor indices = abs(c(:)); - endif - if (ischar (m)) - s = toascii (m); - else - s = m; + ## Since sort is 'stable' the order of identical elements will be + ## preserved, so by traversing the sort indices in reverse order we + ## will make sure that identical elements in index i are subsorted by + ## index j. + indices = flipud (indices); + mode = flipud (mode'); + i = [1:size(m,1)]'; + for ii = 1:length (indices); + [trash, idx] = sort (m(i, indices(ii)), mode{ii}); + i = i(idx); + endfor endif - ## Since sort is 'stable' the order of identical elements will be - ## preserved, so by traversing the sort indices in reverse order we - ## will make sure that identical elements in index i are subsorted by - ## index j. - indices = flipud (indices); - mode = flipud (mode'); - i = [1:size(m,1)]'; - for ii = 1:length (indices); - [trash, idx] = sort (s(:,indices(ii)), mode{ii}); - s = s(idx,:); - i = i(idx); - endfor - - if (ischar (m)) - s = char (s); - endif + s = m(i,:); endfunction