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

  1. % function out = msub(mat1,mat2,...,matN)
  2. %
  3. %   Subtract SYSTEM/VARYING/CONSTANT matrices, currently limited
  4. %   to 9 input arguments.
  5. %
  6. %   out = mat1 - mat2 - mat3 - ... - matN
  7. %
  8. %   See also: ABV, MADD, DAUG, MMULT, and MSCL.
  9.  
  10. function out = msub(mat1,mat2,mat3,mat4,mat5,mat6,mat7,mat8,mat9)
  11.   if nargin <= 1
  12.     disp('usage: out = msub(mat1,mat2,...,matN)')
  13.     return
  14.   elseif nargin == 2
  15.     [onetype,onerows,onecols,onenum] = minfo(mat1);
  16.     [twotype,tworows,twocols,twonum] = minfo(mat2);
  17.     if onetype == 'empt' & twotype == 'empt'
  18.       out = [];
  19.       return
  20.     end
  21.     if (onetype == 'syst' & twotype == 'vary') | ...
  22.           (onetype == 'vary' & twotype == 'syst')
  23.        error('subtraction of SYSTEM and VARYING not allowed')
  24.        return
  25.     elseif onerows ~= tworows | onecols ~= twocols
  26.       error('incompatible dimensions in MSUB')
  27.       return
  28.     end
  29.     if onetype == 'vary'
  30.       if twotype == 'vary'
  31. %       both varying
  32.         code = indvcmp(mat1,mat2);
  33.         if code == 1
  34.           [nr,nc] = size(mat1);
  35.           [vd1,rp1,indv1] = vunpck(mat1);
  36.           [vd2,rp2,indv2] = vunpck(mat2);
  37.           out = vpck(vd1-vd2,indv1);
  38.         else
  39.           error(['inconsistent varying data'])
  40.           return
  41.         end
  42.       else
  43. %       varying-constant
  44.         [vd1,rp1,indv1] = vunpck(mat1);
  45.         out = vpck(vd1-kron(ones(onenum,1),mat2),indv1);
  46.       end
  47.     elseif onetype == 'cons'
  48.       if twotype == 'vary'
  49. %       constant-varying
  50.         [vd2,rp2,indv2] = vunpck(mat2);
  51.         out = vpck(kron(ones(twonum,1),mat1)-vd2,indv2);
  52.       elseif twotype == 'cons'
  53. %       constant-constant
  54.         out = mat1 - mat2;
  55.       else
  56. %       constant-system
  57.         [a,b,c,d] = unpck(mat2);
  58.         out = pck(a,b,-c,mat1-d);
  59.       end
  60.     else
  61.       if twotype == 'cons'
  62. %       system-constant
  63.         [a,b,c,d] = unpck(mat1);
  64.         out = pck(a,b,c,d-mat2);
  65.       else
  66. %       system-system
  67.         [a1,b1,c1,d1] = unpck(mat1);
  68.         [a2,b2,c2,d2] = unpck(mat2);
  69.         zer = zeros(twonum,onenum);
  70.         a = [a1 zer' ; zer a2];
  71.         b = [b1 ; -b2];
  72.         c = [c1 c2];
  73.         d = d1-d2;
  74.         out = pck(a,b,c,d);
  75.       end
  76.     end
  77.   else
  78. %   recursive call for multiple input arguments
  79.      exp = ['out=msub(msub('];
  80.      for i=1:nargin-2
  81.        exp=[exp 'mat' int2str(i) ','];
  82.      end
  83.      exp = [exp 'mat' int2str(nargin-1) '),mat' int2str(nargin) ');'];
  84.      eval(exp);
  85.   end
  86. %
  87. % Copyright MUSYN INC 1991,  All Rights Reserved
  88.