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

%	L. Ljung 10-2-90,2-7-92
%	Copyright (c) 1990 by the MathWorks, Inc.
%	All Rights Reserved.

[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'

	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=nd+n1;%nd=sum(sum(ms1==NaN)');
	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
	kk=[kk find(abs(rr-ar(arg(nr,1),arg(nr,2)))<eps)];
	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'

	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=nd+n1;%nd=sum(sum(ms1==NaN)');
	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
	kk=[kk find(abs(rr-br(arg(nr,1),arg(nr,2)))<eps)];
	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'

	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
	kk=[kk find(abs(rr-cr(arg(nr,1),arg(nr,2)))<eps)];
	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'

	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
	kk=[kk find(abs(rr-dr(arg(nr,1),arg(nr,2)))<eps)];
	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'

	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
	kk=[kk find(abs(rr-kr(arg(nr,1),arg(nr,2)))<eps)];
	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'

	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=nd+n1;%nd=sum(sum(ms1==NaN)');
	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
	kk=[kk find(abs(rr-x0r(arg(nr,1),arg(nr,2)))<eps)];
	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

