function [ms1,par1]=unfixxx(ms,par,mat,arg)
%UNFIXXX Subroutine for UNFIXPAR
%
%	[MS1,PAR1] = unfixxx(MS,PAR,MAT,ARG)
%

%	L. Ljung 10-2-90,3-13-93
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.2 $  $Date: 1995/01/24 00:54:03 $

[nx,nn]=size(ms);if ms(1,nn)<0,arx=1;else arx=0;end
[a,b,c,d,k,x0]=ssmodx9(par,-1,ms);
par1=par;

nd=length(par);[n1,n2]=size(arg);

[rarg,carg]=size(arg);
[as,bs,cs,ds,ks,x0s]=ssmodx9(NaN*ones(1,nd),-1,ms);
if mat=='a' | mat=='A'
	as1=as;
	if rarg==0, [nmmr,nmmc]=size(as);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	as(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=a(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end
	rr=rand(1,nd);

	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(as1(arg(nr,1),arg(nr,2))),
	  kk=[kk find(abs(rr-ar(arg(nr,1),arg(nr,2)))<eps)];
	end
	end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end
if mat=='b' | mat=='B'
	bs1=bs;
	if rarg==0, [nmmr,nmmc]=size(bs);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	bs(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=b(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end

	rr=rand(1,nd);
	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(bs1(arg(nr,1),arg(nr,2)))
	kk=[kk find(abs(rr-br(arg(nr,1),arg(nr,2)))<eps)];
	end,end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end
if mat=='c' | mat=='C'
	cs1=cs;
	if rarg==0, [nmmr,nmmc]=size(cs);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	cs(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=c(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end

	rr=rand(1,nd);
	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(cs1(arg(nr,1),arg(nr,2)))
	kk=[kk find(abs(rr-cr(arg(nr,1),arg(nr,2)))<eps)];
	end,end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end
if mat=='d' | mat=='D'
	ds1=ds;
	if rarg==0, [nmmr,nmmc]=size(ds);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	ds(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=d(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end

	rr=rand(1,nd);
	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(ds1(arg(nr,1),arg(nr,2)))
	kk=[kk find(abs(rr-dr(arg(nr,1),arg(nr,2)))<eps)];
	end,end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end
if mat=='k' | mat=='K'
	ks1=ks;
	if rarg==0, [nmmr,nmmc]=size(ks);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	ks(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=k(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end

	rr=rand(1,nd);
	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(ks1(arg(nr,1),arg(nr,2)))
	kk=[kk find(abs(rr-kr(arg(nr,1),arg(nr,2)))<eps)];
	end,end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end
if mat=='x0' | mat=='X0'
	x0s1=x0s;
        if rarg==0, [nmmr,nmmc]=size(x0s);
	for kclr=1:nmmr,for kclc=1:nmmc
        arg=[arg;[kclr,kclc]];
	end,end
	[rarg,rarc]=size(arg);
	end

	kk=[];
	for nr=1:rarg
	x0s(arg(nr,1),arg(nr,2))=NaN;
	pp(nr)=x0(arg(nr,1),arg(nr,2));
	end
	ms1=modstruc(as,bs,cs,ds,ks,x0s);
	nd=sum(sum(isnan(ms1))');
	if arx,ms1(1,nn)=-ms1(1,nn);end

	rr=rand(1,nd);
	[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms1);
	for nr=1:rarg
	if ~isnan(x0s1(arg(nr,1),arg(nr,2)))
	kk=[kk find(abs(rr-x0r(arg(nr,1),arg(nr,2)))<eps)];
	end,end
	ksort=sort(kk);lks=length(ksort);
	for ll=1:lks
par1=[par1(1:ksort(ll)-1) 0 par1(ksort(ll):length(par1))];

	end
	for ll=1:lks
	par1(kk(ll))=pp(ll);
	end
end

