Mercurial > hg > octave-nkf
diff scripts/general/sortrows.m @ 5181:41cd70503c72
[project @ 2005-03-03 05:49:55 by jwe]
author | jwe |
---|---|
date | Thu, 03 Mar 2005 05:49:55 +0000 |
parents | 6758c11b5b99 |
children | 5b361aa47dff |
line wrap: on
line diff
--- a/scripts/general/sortrows.m +++ b/scripts/general/sortrows.m @@ -1,18 +1,21 @@ ## Copyright (C) 2000 Daniel Calvelo ## -## This program is free software; you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2 of the License, or -## (at your option) any later version. +## This file is part of Octave. ## -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2, or (at your option) +## any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. ## B = sortrows (A) ## returns matrix with rows sorted "lexicographically" @@ -24,38 +27,37 @@ ## Set implicit_str_to_num_ok and implicit_num_to_str_ok to 1 if you ## use this for string sorting and octave 2.1.50 or earlier. -## Author: Daniel Calvelo -## 2001-02-24 Paul Kienzle -## * cleanup according to Octave conventions -## * return reverse index -## * handle string arguments +## Author: Daniel Calvelo, Paul Kienzle +## Adapted-by: jwe function [s, i] = sortrows (m, c) - if nargin < 2 - indices = [ 1 : size(m,2) ]'; + if (nargin < 2) + indices = [1:size(m,2)]'; else - indices = c (:); + indices = c(:); endif - if (isstr (m)) + if (isstr (m)) s = toascii (m); else s = m; 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. + ## 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); - i = [1 : size(m,1)]'; - for ii = 1 : length (indices); - [ trash, idx ] = sort ( s ( : , indices (ii) ) ); - s = s ( idx, : ); - i = i (idx ); + i = [1:size(m,1)]'; + for ii = 1:length (indices); + [trash, idx] = sort (s(:,indices(ii))); + s = s(idx,:); + i = i(idx); endfor + if (isstr (m)) - s = setstr(s); + s = setstr (s); endif + endfunction