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

  1. function X=chop(Xin,n,unit)
  2. % CHOP  CHOP(X,n) rounds the elements of X to n significant 
  3. %       figures.
  4. %       
  5. %       e.g. chop(3.141592,5) returns 3.141600000..
  6. %
  7. %       CHOP(X,n,unit) rounds the elements of X to n significant
  8. %     figures whose digits (mantissa) are exactly divisible
  9. %    by unit. 
  10. %
  11. %       e.g. chop(3.141592,3,5) returns 3.150000000..
  12. %            chop(3.141592,3,3) returns 3.150000000..
  13. %            chop(3.141592,3,2) returns 3.140000000..
  14. %       
  15.  
  16. %    Copyright (c) 1986-93 by the MathWorks, Inc.
  17.  
  18. % Set last sig. fig. rounding to 1 if only two input arguments.
  19. if nargin<3, unit=1; end
  20.  
  21. % Cater for -ve numbers  and numbers = 0.
  22. X=abs(Xin) +(Xin==0);
  23. [nx,mx] = size(X);
  24. exponent=unit.*((10*ones(nx,mx)).^(floor(log10(X))-n+1));
  25. X=round(X./exponent).*exponent;
  26.  
  27. % Put back sign and zeros
  28. X=sign(Xin).*X.*(Xin~=0);
  29.