Mercurial > hg > octave-lyh
changeset 7678:5988e08c1ae6
handle negative column values in sortrows
author | Richard Bovey |
---|---|
date | Tue, 01 Apr 2008 16:02:49 -0400 |
parents | db02cc0ba8f2 |
children | 3c16e2414564 |
files | doc/interpreter/contributors.in scripts/ChangeLog scripts/general/sortrows.m |
diffstat | 3 files changed, 27 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/contributors.in +++ b/doc/interpreter/contributors.in @@ -13,6 +13,7 @@ Don Bindner Jakub Bogusz Moritz Borgmann +Richard Bovey Marcus Brinkmann Remy Bruno Marco Caliari
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2008-04-01 Richard Bovey <Richard.Bovey@baesystems.com> + + * general/sortrows.m: Handle negative column arguments. + 2008-04-01 Julian Schnidder <j.schnidder@gmx.de> * miscellaneous/perl.m: New function.
--- a/scripts/general/sortrows.m +++ b/scripts/general/sortrows.m @@ -20,18 +20,30 @@ ## @deftypefn {Function File} {} sortrows (@var{a}, @var{c}) ## Sort the rows of the matrix @var{a} according to the order of the ## columns specified in @var{c}. If @var{c} is omitted, a -## lexicographical sort is used. +## lexicographical sort is used. By default ascending order is used +## however if elements of @var{c} are negative then the corrosponding +## column is sorted in descending order. ## @end deftypefn ## Author: Daniel Calvelo, Paul Kienzle ## Adapted-by: jwe function [s, i] = sortrows (m, c) - + + default_mode = "ascend"; + other_mode = "descend"; if (nargin < 2) indices = [1:size(m,2)]'; + mode{1:size(m,2)} = default_mode; else - indices = c(:); + for ii = 1:length (c); + if (c(ii) < 0) + mode{ii} = other_mode; + else + mode{ii} = default_mode; + endif + endfor + indices = abs(c(:)); endif if (ischar (m)) @@ -45,9 +57,10 @@ ## 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))); + [trash, idx] = sort (s(:,indices(ii)), mode{ii}); s = s(idx,:); i = i(idx); endfor @@ -57,3 +70,8 @@ endif endfunction + +%!shared x, idx +%! [x, idx] = sortrows ([1, 1; 1, 2; 3, 6; 2, 7], [1, -2]); +%!assert (x, [1, 2; 1, 1; 2, 7; 3, 6]); +%!assert (idx, [2; 1; 4; 3]);