home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / control / sortcom.m < prev    next >
Encoding:
Text File  |  1999-12-15  |  2.5 KB  |  86 lines

  1. ## Copyright (C) 1996 Auburn University.  All Rights Reserved.
  2. ##
  3. ## This file is part of Octave. 
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it 
  6. ## under the terms of the GNU General Public License as published by the 
  7. ## Free Software Foundation; either version 2, or (at your option) any 
  8. ## later version. 
  9. ## 
  10. ## Octave is distributed in the hope that it will be useful, but WITHOUT 
  11. ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  12. ## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  13. ## for more details.
  14. ## 
  15. ## You should have received a copy of the GNU General Public License 
  16. ## along with Octave; see the file COPYING.  If not, write to the Free 
  17. ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. 
  18.  
  19. ## -*- texinfo -*-
  20. ## @deftypefn {Function File } { outputs =} sortcom ( inputs ) 
  21. ## @format
  22. ##  [yy,idx] = sortcom(xx[,opt]): sort a complex vector
  23. ##  xx: complex vector
  24. ##  opt: sorting option:
  25. ##     "re": real part (default)
  26. ##     "mag": by magnitude
  27. ##     "im": by imaginary part
  28. ## 
  29. ##   if opt != "im" then complex conjugate pairs are grouped together,
  30. ##      a - jb followed by a + jb.
  31. ##  yy: sorted values
  32. ##  idx: permutation vector: yy = xx(idx)
  33. ## 
  34. ## 
  35. ## @end format
  36. ## @end deftypefn
  37.  
  38. function [yy, idx] = sortcom (xx, opt)
  39.  
  40.   ## Written by A. S. Hodel June 1995
  41.  
  42.   if( nargin < 1 | nargin > 2 )
  43.      usage("yy = sortcom(xx[,opt]");
  44.   elseif( !(is_vector(xx) | isempty(xx) ))
  45.     error("sortcom: first argument must be a vector");
  46.   endif
  47.  
  48.   if(nargin == 1)         opt = "re";
  49.   else
  50.     if (!isstr(opt))
  51.       error("sortcom: second argument must be a string");
  52.     endif
  53.   endif
  54.  
  55.   if(isempty(xx))
  56.     yy = idx = [];
  57.   else
  58.     if(strcmp(opt,"re"))        datavec = real(xx);
  59.     elseif(strcmp(opt,"im"))    datavec = imag(xx);
  60.     elseif(strcmp(opt,"mag"))   datavec = abs(xx);
  61.     else                        error(["sortcom: illegal option = ", opt])
  62.     endif
  63.   
  64.     [datavec,idx] = sort(datavec);
  65.     yy= xx(idx);
  66.     
  67.     if(strcmp(opt,"re") | strcmp(opt,"mag"))
  68.       ## sort so that complex conjugate pairs appear together
  69.       
  70.       ddiff = diff(datavec);
  71.       zidx = find(ddiff == 0);
  72.   
  73.       ## sort common datavec values
  74.       if(!isempty(zidx))
  75.         for iv=create_set(datavec(zidx))
  76.           vidx = find(datavec == iv);
  77.           [vals,imidx] = sort(imag(yy(vidx)));
  78.           yy(vidx)  = yy(vidx(imidx));
  79.           idx(vidx) = idx(vidx(imidx));
  80.         endfor
  81.       endif
  82.     endif
  83.   endif  
  84. endfunction
  85.   
  86.