function [n, r, m] = abcddim (A, B, C, D)
% Usage: [n, r, m] = abcddim (A, B, C, D)
%
% Check for compatibility of the dimensions of the matrices defining
% the linear system (A, B, C, D).
%
% Returns n = number of system states,
%         r = number of system inputs,
%         m = number of system outputs.
%
% Returns n = r = m = -1 if the system is not compatible.

% A.S. Hodel <scotte@eng.auburn.edu>

  if (nargin ~= 4)
    error ('usage: abcddim (A, B, C, D)');
  end

  n = -1; r = -1; m = -1;

  [an, am] = size(A);
  if (an ~= am), error ('abcddim: A is not square'); end

  [bn, br] = size(B);
  if (bn ~= an)
     error (sprintf('abcddim: A and B are not compatible, A:(%dx%d) B:(%dx%d)',am,an,bn,br))
  end

  [cm, cn] = size(C);
  if (cn ~= an) 
     error (sprintf('abcddim: A and C are not compatible, A:(%dx%d) C:(%dx%d)',am,an,cm,cn))
  end

  [dm, dr] = size(D);
  if (cm ~= dm)
     error (sprintf('abcddim: C and D are not compatible, C:(%dx%d) D:(%dx%d)',cm,cn,dm,dr))
  end
  if (br ~= dr)
     error (sprintf('abcddim: B and D are not compatible, B:(%dx%d) D:(%dx%d)',bn,br,dm,dr))
  end

  n = an;
  r = br;
  m = cm;

% -------------------------------------------------------------- abcddim.m
