# HG changeset patch # User Richard Bovey # Date 1207080224 14400 # Node ID 69c8f4cdd4727218cdb71501abb8bb8d49198308 # Parent 8959d5f5c2b1ec4254c652800ecabf7fd6609553 handle negative column values in sortrows diff -r 8959d5f5c2b1 -r 69c8f4cdd472 doc/interpreter/contributors.in --- a/doc/interpreter/contributors.in Mon Mar 31 22:13:32 2008 -0400 +++ b/doc/interpreter/contributors.in Tue Apr 01 16:03:44 2008 -0400 @@ -13,6 +13,7 @@ Don Bindner Jakub Bogusz Moritz Borgmann +Richard Bovey Marcus Brinkmann Remy Bruno Marco Caliari diff -r 8959d5f5c2b1 -r 69c8f4cdd472 scripts/ChangeLog --- a/scripts/ChangeLog Mon Mar 31 22:13:32 2008 -0400 +++ b/scripts/ChangeLog Tue Apr 01 16:03:44 2008 -0400 @@ -1,3 +1,7 @@ +2008-04-01 Richard Bovey + + * general/sortrows.m: Handle negative column arguments. + 2008-03-31 David Bateman * plot/__go_draw_axes__.m: Set the tick direction in the main call diff -r 8959d5f5c2b1 -r 69c8f4cdd472 scripts/general/sortrows.m --- a/scripts/general/sortrows.m Mon Mar 31 22:13:32 2008 -0400 +++ b/scripts/general/sortrows.m Tue Apr 01 16:03:44 2008 -0400 @@ -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]);