help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting of words


From: etienne grossmann
Subject: Re: Sorting of words
Date: Fri, 25 Feb 2000 18:08:34 +0000 (WET)

  Hello,


> I use Octave 2.0.16 and I have the following problem. I want to sort
> alphabetically the words: "apple", "dog", "long". I tried with SORT
> function without success. How can I solve this problem?

  If there is no better way, this should do :

octave:26> a = ["axy";"along";"apple";"app"] ;
octave:27> setstr( lexicosort(toascii(a)) )
ans =

along
app  
apple
axy  

   lexicosort is the home-made func below. Not thoroughly tested, so
be prudent.

  Cheers,

  Etienne

======================================================================
##       [vv,ii] = lexicosort(v) 
##
## Sort lexicographicaly the rows of matrix v
## 
## Author : Etienne Grossmann (address@hidden)
function [vv,ii] = lexicosort(v) 

  [R,C] = size(v) ;
  if C<=1,                      # Single column
    [vv,ii] = sort(v);
    return
  elseif R <= 1,                # Single row (apparently, no need to
                                # treat as a special case)
    vv = v ;
    ii = 1 ;
    return
  end

  vv = v ;
  if any(v(:)<0) ,              # Here, could be improved
    v = v-min(v(:)) ;
  end

  mv = max(v)+1 ;

  cv = fliplr(cumprod(fliplr(mv))) ;
  cv = [cv(2:size(cv,2)),1] ;
  ## cv = cumprod(mv)./mv(1) ;

  v2 = sum( (v .* (ones(R,1)*cv))' )  ;
  ## keyboard
  [v3,ii] = sort(v2) ;
  vv = vv(ii,:) ;

  ## Tested with
  ## v = floor(10*rand(5,2))
  ## [vv,ii]=lexicosort(v)  
  ## [vv,ii]=lexicosort(v-5)
  ## [vv,ii]=lexicosort(v(:,1))



-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
Subscription information:  http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]