%
%  Gs = selstruct(G, S);
%
%  Function to select a certain structure in G described by S,
%  that is selects in G all elements in S where 1 appears and
%  removes the elements where 0 appears in S.
%
%  Written by: Kjetil Havre 15/8-1996.
%  
function Gs = selstruct(G, S);
   [mt,rd,cd,nx]     = minfo(G);
   [mts,rds,cds,nxs] = minfo(S);

   if rd ~= rds
      disp('Number of rows in G and S must be equal.');
      return
   end
   if cd ~= cds
      disp('Number of columns in G and S must be equal.');
      return
   end

   if mts ~= 'cons'
     disp('The structure matrix must be a constant matrix.');
     return
   end

   [A,B,C,D] = unpck(G);
   
   gs = [];
   for i=1:rd,
     gj = [];
     for j=1:cd,
       if S(i,j) == 1
	 a = A;
	 b = B(:,j);
	 c = C(i,:);
	 d = D(i,j);
	 gij = sminreal(pck(a,b,c,d));
       else  	 
	 gij = pck([],[],[],0);
       end
       gj = sminreal(sbs(gj,gij));
     end
     gs = sminreal(abv(gs,gj));
   end
   Gs = gs;
end
 





