function [out,mat,delta] = randumat(NY,NU);
%RANDUMAT  Generates random UMAT.
%
%  OUT = randumat(NY,NU) generates an uncertain matrix of size
%  NY-by-NU. RANDUMAT randomly selects between UREAL, UCOMPLEX and ULTIDYN 
%  uncertaint atoms.
%
%  If RANDUMAT is called with no input arguments, a 1-by-1 uncertain matrix
%  is generated with up to 4 uncertain objects.
%
%  See also RAND, RANDN, RANDATOM, RANDUSS, UCOMPLEX, ULTIDYN, UREAL. 

% Copyright 2003-2004 The MathWorks, Inc.

if nargin == 0
   NY = 1;
   NU = 1;
elseif nargin == 1
   NU = NY;
end

if isa(NY,'double') && length(NY)==1 && NY>0 && floor(NY)==ceil(NY)
   % NY is an integer
else
   error('NY must be a positive integer')
end
if isa(NU,'double') && length(NU)==1 && NU>0 && floor(NU)==ceil(NU)
   % NU is an integer
else
   error('NU must be a positive integer')
end

blktype = {'ureal' 'ucomplex' 'ultidyn'};
delta = [];
mat = [];
nblks = ceil(4*rand);
for i=1:nblks
   blk = blktype{ceil(2*rand)};
   if i==1
      blk = 'ucomplex';
   end   
   if isequal(blk,'ultidyn')
      at = randatom(blk);
      delta = blkdiag(delta,at);      
      szat = size(at);
      mat22 = rand([szat(2) szat(1)]);
      mat22 = mat22/norm(mat22)*0.5*rand;
      N = pvget(at,'Normalizer');
      mat22 = lft(N.T,mat22);    
      row = szat(1);
      col = szat(2);
   else
      ncop = ceil(3*rand);
      at = randatom(blk);
      for j=1:ncop
         delta = blkdiag(delta,at);
      end
      N = pvget(at,'Normalizer');
      mat22=[];
      for j=1:ncop
         if isequal(blktype,'ureal')
            tmp = randn;
         else
            tmp = randn+j*randn;
         end
         tmp = tmp/norm(tmp)*0.5*rand;            
         mat22 = blkdiag(mat22,lft(N.T,tmp));         
      end
      row = ncop; 
      col = ncop;
   end
   mat = [mat 0.2*randn(size(mat,1),row);   zeros(col,size(mat,2)) mat22];
end
mat = [mat randn(size(mat,1),NU); randn(NY,size(mat,2)+NU)];
out = lft(delta,mat);
