% function out = sel(mat,rowdata,coldata)
%
%   selects rows and columns from a VARYING matrix
%   and outputs and inputs from a SYSTEM matrix.
%
%   See also: GETIV, REORDSYS, SRESID, TRUNC, UNPCK, 
%             VUNPCK, XTRACT and XTRACTI.

function [out] = sel(mat,rdata,cdata)
 if nargin ~= 3
   disp('usage: out = sel(mat,rowdata,coldata)')
   return
 end
 if min(size(rdata)) ~= 1 | min(size(cdata)) ~= 1
   error('ROWDATA and COLDATA should be vectors')
   return
 end 
 [nr,nc] = size(mat);
 [mtype,mrows,mcols,mnum] = minfo(mat);
 if mtype == 'vary'
   if max(cdata) <= mcols & min(cdata) > 0 ...
     & max(rdata) <= mrows & min(rdata) > 0
     npts = mnum;
     nrout = max(size(rdata));
     ncout = max(size(cdata));
     out = zeros(nrout*npts+1,ncout+1);
     out(nrout*npts+1,ncout+1) = inf;
     out(nrout*npts+1,ncout) = npts;
     out(1:npts,ncout+1) = mat(1:mnum,nc);
     ff = (npts+1)*mrows;
     fout = (npts+1)*nrout;
     pp = 1:mrows:ff;
     pout = 1:nrout:fout;
     ppm1 = pp(2:npts+1) - 1;
     poutm1 = pout(2:npts+1) - 1; 
     for i=1:npts
       out(pout(i):poutm1(i),1:ncout) = mat((pp(i)-1)+rdata,cdata);
     end
   else
     error('column or row data out of range')
     return
   end
 elseif mtype == 'syst'
   if max(cdata) <= mcols & min(cdata) > 0 ...
     & max(rdata) <= mrows & min(rdata) > 0
     cols = [1:mnum mnum+cdata nc];
     rows = [1:mnum mnum+rdata nr];
     out = mat(rows,cols);
   else
     error('input or output data out of range')
     return
   end
 elseif mtype == 'cons'
   if max(cdata) <= mcols & min(cdata) > 0 ...
      & max(rdata) <= mrows & min(rdata) > 0
     out = mat(rdata,cdata);
   else
     error('column or row data out of range')
     return
   end
 else
   out = [];
 end
%
% Copyright MUSYN INC 1991,  All Rights Reserved
