home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 10.ddi / CONTROL.DI$ / VSORT.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  911 b   |  35 lines

  1. function [v2,pind]=vsort(v1,v2,sp)
  2. % VSORT Matches two vectors.  Used in RLOCUS.
  3. %    VS2 = VSORT(V1,V2) matches two complex vectors 
  4. %    V1 and V2, returning VS2 with consists of the elements 
  5. %    of V2 sorted so that they correspond to the elements 
  6. %    in V1 in a least squares sense.
  7.  
  8. %         [v2,pind]=VSORT(v1,v2,sp) 
  9. %       sp is used to test a quick sort method and is equal to
  10. %       sp=sum([1:length(indr)].^2); pind=1 is returned if
  11. %       a slow sort method has to be applied.
  12.  
  13. %    Copyright (c) 1986-93, by the MathWorks, Inc.
  14.  
  15. pind=0;
  16. if nargin < 3, sp = sum([1:length(v1)].^2); end
  17. % Quick Sort 
  18. p=length(v2);
  19. vones=ones(p,1);
  20. [dum,indr1]=min(abs(vones*v2.'-v1*vones'));
  21. indr(indr1)=[1:p];
  22.  
  23. % Long (accurate) sort
  24. if (indr*indr' ~= sp) 
  25.     [dum,jnx]=sort(abs(v2));
  26.     pind=1;
  27.     for j=jnx' 
  28.         [dum,inx]=min(abs(v2(j)-v1));
  29.         indr(inx)=j;
  30.         v1(inx)=1e30;
  31.     end
  32.  
  33. end
  34. v2=v2(indr);
  35.