home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / sortcom.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  80 lines

  1. # Copyright (C) 1996 A. Scottedward Hodel 
  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. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function [yy,idx] = sortcom(xx,opt)
  18. # [yy,idx] = sortcom(xx[,opt]): sort a complex vector
  19. # xx: complex vector
  20. # opt: sorting option:
  21. #    "re": real part (default)
  22. #    "mag": by magnitude
  23. #    "im": by imaginary part
  24. #
  25. #  if opt != "im" then values with common real part/magnitude are
  26. #     sorted by imaginary part, i.e. a - jb followed by a + jb. 
  27. #     [Complex conjugate pairs may not be grouped consecutively if more than 2
  28. #     numbers share a common real part/magnitude]
  29. # yy: sorted values
  30. # idx: permutation vector: yy = xx(idx)
  31.  
  32. # Written by A. S. Hodel June 1995
  33.  
  34.   if( nargin < 1 | nargin > 2 )
  35.      usage("yy = sortcom(xx[,opt]");
  36.   elseif( !(is_vec(xx) | isempty(xx) ))
  37.     error("sortcom: first argument must be a vector");
  38.   endif
  39.  
  40.   if(nargin == 1)         opt = "re";
  41.   else
  42.     if (!isstr(opt))
  43.       error("sortcom: second argument must be a string");
  44.     endif
  45.   endif
  46.  
  47.   if(isempty(xx))
  48.     yy = idx = [];
  49.   else
  50.     if(strcmp(opt,"re"))        datavec = real(xx);
  51.     elseif(strcmp(opt,"im"))    datavec = imag(xx);
  52.     elseif(strcmp(opt,"mag"))   datavec = abs(xx);
  53.     else                        error(["sortcom: illegal option = ", opt])
  54.     endif
  55.   
  56.     [datavec,idx] = sort(datavec);
  57.     yy= xx(idx);
  58.     
  59.     if(strcmp(opt,"re") | strcmp(opt,"mag"))
  60.       # sort so that complex conjugate pairs appear together
  61.       
  62.       ddiff = diff(datavec);
  63.       zidx = find(ddiff == 0);
  64.   
  65.       # sort common datavec values
  66.       if(!isempty(zidx))
  67.         for iv=crt_set(datavec(zidx))
  68.           vidx = find(datavec == iv);
  69.           [vals,imidx] = sort(imag(yy(vidx)));
  70.           yy(vidx)  = yy(vidx(imidx));
  71.           idx(vidx) = idx(vidx(imidx));
  72.         endfor
  73.       endif
  74.     endif
  75.   endif  
  76. endfunction
  77.   
  78.