function [a,b,c,d]=selector(A,B,C,D,xselect,uselect,yselect)
% function [a,b,c,d]=selector(A,B,C,D,xselect,uselect,yselect)
%
% This function pulls a small state-space subsystem (defined by matrices a,b,c,d) from 
% a large state-space system (defined by matrices A,B,C,D).
% xselect indicates which of the elements of the state vector corresponding to system matrix A
% that you want to retain in the subsystem with system matrix a.
% uselect  indicates which of the elements of the control vector corresponding to input matrix B
% that you want to retain in the subsystem with input matrix b.
% yselect  indicates which of the elements of the output vector corresponding to output matrix C
% that you want to retain in the subsystem with output matrix c.
% 
% Example: xselect=[1,4,5], uselect=[2,3], yselect=[2] creates a subsystem
% consisting of the first, fourth and fifth states, the second and third control
% and the second output from the original state space.
% 
% The subsystem needs to lend itself to decompostition from the larger state-space 
% for the subsystem to make any physical sense.

m=length(xselect);
a=zeros(m);
for ii=1:m
    for jj=1:m
        a(ii,jj)=A(xselect(ii),xselect(jj));
    end
end

n=length(uselect);
b=zeros(m,n);
for ii=1:m
    for jj=1:n
        b(ii,jj)=B(xselect(ii),uselect(jj));
    end
end

k=length(yselect);
c=zeros(k,m);
for ii=1:k
    for jj=1:m
        c(ii,jj)=C(yselect(ii),xselect(jj));
    end
end

d=zeros(k,n);
for ii=1:k
    for jj=1:n
        d(ii,jj)=D(yselect(ii),uselect(jj));
    end
end
