function [z,p] = getzp(zepo,ku,ky)
%GETZP	Extracts the zeros and the poles from the ZEPO-format created by TH2ZP
%
%	[Z,P] = getzp(ZEPO,KU,KY)
%	
%	Z : The zeros
%	P : The poles
%	ZEPO: The matrix of zeros and poles, resulting from th2zp
%	KU : The input number (default 1). Noise source #ju is represented as
%	     input number -ju.
%	
%	KY : The output number (default 1).
%
%	If several entries in ZEPO correspond to the same input-output
%	relation, then Z and P will contain one column for each model.
%	Z and P will be stripped from spurious 'inf':s and zeros as far
%	as possible.

%	L. Ljung 20-8-90
%	Copyright (c) 1990 by the MathWorks, Inc.
%	All Rights Reserved.

if nargin<3, ky=[];end
if nargin<2, ku=[];end
if isempty(ky), ky=1;end
if isempty(ku), ku=1;end
if ku==0,ku=-1;end
if ku<0, ku=500+abs(ku);end
zind = find(abs(zepo(1,:))==(ky-1)*1000+ku);
pind = find(abs(zepo(1,:))==(ky-1)*1000+ku+20);
if isempty(zind),disp('No zeros corresponding to this input/output pair found'),else
zer = zepo(:,zind); [zr,zc]=size(zer); disc=zer(1,1)>0;
zer=zer(2:zr,:);
z=[];
for kc=1:zc
z1 = zer(find(zer(:,kc)~=inf),kc);
if disc, z1 = z1(find(z1~=0)); end
l1=length(z1);[zzr,zzc]=size(z);mr=max(l1,zzr);
z = [[z;ones(mr-zzr,zzc)+inf],[z1;ones(mr-l1,1)+inf]];
end,end
if isempty(pind),disp('No poles corresponding to this input/output pair found'),else
pol = zepo(:,pind); [zr,zc]=size(pol); disc=pol(1,1)>0;
pol=pol(2:zr,:);
p=[];
for kc=1:zc
z1 = pol(find(pol(:,kc)~=inf),kc);
if disc, z1 = z1(find(z1~=0)); end
l1=length(z1);[zzr,zzc]=size(p);mr=max(l1,zzr);
p = [[p;ones(mr-zzr,zzc)+inf],[z1;ones(mr-l1,1)+inf]];
end,end
