home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 2.ddi / MUTOOLS1.DI$ / XTRACT.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.6 KB  |  90 lines

  1. % function [matout,err] = xtract(mat,ivlow,ivhigh)
  2. %  or
  3. % function [matout,err] = xtract(mat,desiv)
  4. %
  5. %   Extract specified portion of a VARYING matrix, with
  6. %   INDEPENDENT VARIABLE's values between IVLOW and IVHIGH.
  7. %   XTRACT assumes that the INDEPENDENT VARIABLE value's are
  8. %   sorted in ascending order. If only two input arguments 
  9. %   are used, then the matrix whose INDEPENDENT VARIABLE's 
  10. %   value is closest to the second input argument is 
  11. %   extracted as a VARYING matrix (with one point).
  12. %
  13. %   ERR is set to -1 if IVLOW is greater than IVHIGH, ERR is
  14. %   set to -2 if there are no INDEPENDENT VARIABLE values in
  15. %   the range selected. ERR is set to -3 if the input matrix
  16. %   is not a VARYING matrix. on a successful call, ERR is 
  17. %   set to 0.
  18. %
  19. %   See also: SEL, VAR2CON, VPCK, VUNPCK, and XTRACTI.
  20.  
  21. function [matout,err] = xtract(mat,wlow,whigh)
  22.   err = 0;
  23.   if nargin < 2
  24.     disp('usage: [matout,err] = xtract(mat,ivlow,ivhigh)')
  25.     return
  26.   end
  27.   if nargin == 3
  28.     if wlow > whigh
  29.       if nargout == 2
  30.         err = -1;
  31.         return
  32.       else
  33.         error(['ivlow should be <= ivhigh'])
  34.         return
  35.       end
  36.     end
  37.   end
  38.   [mtype,mrows,mcols,mnum] = minfo(mat);
  39.   if mtype == 'vary'
  40.     [nr,nc]=size(mat);
  41.     omega = mat(1:mnum,nc);
  42.     if nargin == 2
  43.       [val,loc] = min(abs(omega-wlow*ones(mnum,1)));
  44.       matout = vpck(mat((loc-1)*mrows+1:mrows*loc,1:mcols),mat(loc,mcols+1));
  45.     else
  46.       if max(size(omega)) == 1
  47.         if omega(1) <= whigh & wlow <= omega(1)
  48.          matout = mat;
  49.         else
  50.           if nargout == 2
  51.             err = -2;
  52.             out = [];
  53.             return
  54.           else
  55.             error(['no INDEPENDENT VARIABLES in range selected'])
  56.             return
  57.           end
  58.         end
  59.       end
  60.       % change ones(omega) into ones(size(omega)) Wes Wang, the MathWorks
  61.       key = omega>=ones(size(omega))*wlow & omega<=ones(size(omega))*whigh;
  62.       ptlow = min(find(key));
  63.       pthigh = max(find(key));
  64.       pt = [ptlow pthigh];
  65.       if ~isempty(pt)
  66.         matout = mat(mrows*(pt(1)-1)+1:mrows*pt(2),1:nc-1);
  67.         newomega = omega(pt(1):pt(2));
  68.         matout=vpck(matout,newomega);
  69.       else
  70.         if nargout == 2
  71.           err = -2;
  72.           return
  73.         else
  74.           error(['no INDEPENDENT VARIABLES in range selected'])
  75.           return
  76.         end
  77.       end
  78.     end
  79.   else
  80.     if nargout == 2
  81.       err = -3;
  82.       return
  83.     else
  84.       error(['input is not a valid VARYING matrix'])
  85.       return
  86.     end
  87.   end
  88. %
  89. % Copyright MUSYN INC 1991,  All Rights Reserved
  90.