function [IOC,skeep] = getIOConnectivity(M,ns)
% Computes input/output connectivity.
%
%                    +-------+
%          u ------->|       |-------> y
%                    |   M   |
%               +--->|       |----+ 
%               |    +-------+    | s
%               |                 |
%               +-----------------+
%
%   [IOC,SKEEP] = GETIOCONNECTIVITY(P,NS) returns the NY-by-NU connectivity
%   matrix IOC between the inputs U and outputs Y. The entry IOC(I,J) is 
%   TRUE if and only if the transfer function from input J to output I is 
%   structurally nonzero, or equivalently if there is a path from input J
%   to output I. The second argument NS specifies the number of feedback 
%   paths S (NS=0 by default). SKEEP is a 3D logical array where SKEEP(:,I,J) 
%   flags the structurally minimal subset of signals S involved in the 
%   transfer function from input J to output I.

%   Author(s): P. Gahinet
%   Copyright 1986-2011 The MathWorks, Inc.
if nargin<2
   ns = 0;
end
[rs,cs] = size(M);
ny = rs-ns;
nu = cs-ns;
skeep = iosmreal(M(ny+1:rs,nu+1:cs),M(ny+1:rs,1:nu),M(1:ny,nu+1:cs),[]);
IOC = (M(1:ny,1:nu)~=0);  % feedthrough
for ct=1:ny*nu
   IOC(ct) = IOC(ct) || any(skeep(:,ct));
end
