function [ms1,par1]=fixxx(ms,par,mat,arg,fixpar)
%FIXXX Subroutine for fixpar
%
%	[MS1,PAR1] = fixxx(MS,PAR,MAT,ARG,FIXPAR)
%

%	L. Ljung 15-4-90,13-3-93
%	Copyright (c) 1986-95 The MathWorks, Inc
%	$Revision: 1.3 $  $Date: 1995/05/02 16:32:08 $

deff=0;
if fixpar(1)=='d', deff=1;end
[a,b,c,d,k,x0]=ssmodx9(par,-1,ms);

par1=par;
nd=length(par);
[rarg,carg]=size(arg);
rr=rand(1,nd);
[ar,br,cr,dr,kr,x0r]=ssmodx9(rr,-1,ms);
[as,bs,cs,ds,ks,x0s]=ssmodx9(NaN*ones(1,nd),-1,ms);
if mat=='a' | mat=='A'
	kk=[];
        if rarg==0, [nmmr,nmmc]=size(as);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(as(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end
	for nr=1:rarg
	if deff,as(arg(nr,1),arg(nr,2))=a(arg(nr,1),arg(nr,2));
	else as(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-ar(arg(nr,1),arg(nr,2)))<eps)];
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end
if mat=='b' | mat=='B'
        if rarg==0, [nmmr,nmmc]=size(bs);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(bs(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end

	kk=[];
	for nr=1:rarg
	if deff,bs(arg(nr,1),arg(nr,2))=b(arg(nr,1),arg(nr,2));
	else bs(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-br(arg(nr,1),arg(nr,2)))<eps)];
	
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end
if mat=='c' | mat=='C'
        if rarg==0, [nmmr,nmmc]=size(cs);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(cs(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end

	kk=[];
	for nr=1:rarg
	if deff,cs(arg(nr,1),arg(nr,2))=c(arg(nr,1),arg(nr,2));
	else cs(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-cr(arg(nr,1),arg(nr,2)))<eps)];
	
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end
if mat=='d' | mat=='D'
        if rarg==0, [nmmr,nmmc]=size(ds);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(ds(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end

	kk=[];
	for nr=1:rarg
	if deff,ds(arg(nr,1),arg(nr,2))=d(arg(nr,1),arg(nr,2));
	else ds(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-dr(arg(nr,1),arg(nr,2)))<eps)];
	
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end
if mat=='k' | mat=='K'
        if rarg==0, [nmmr,nmmc]=size(ks);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(ks(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end

	kk=[];
	for nr=1:rarg
	if deff,ks(arg(nr,1),arg(nr,2))=k(arg(nr,1),arg(nr,2));
	else ks(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-kr(arg(nr,1),arg(nr,2)))<eps)];
	
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end
if mat=='x0' | mat=='X0'
        if rarg==0, [nmmr,nmmc]=size(x0s);
	for kclr=1:nmmr,for kclc=1:nmmc
        if isnan(x0s(kclr,kclc)),arg=[arg;[kclr,kclc]];end
	end,end
	[rarg,rarc]=size(arg);
        if length(fixpar)==1,fixpar=fixpar*ones(1,rarg);end
	end

	kk=[];
	for nr=1:rarg
	if deff,x0s(arg(nr,1),arg(nr,2))=x0(arg(nr,1),arg(nr,2));
	else x0s(arg(nr,1),arg(nr,2))=fixpar(nr);end
	kk=[kk,find(abs(rr-x0r(arg(nr,1),arg(nr,2)))<eps)];
	
	end
	kk=sort(kk);nk=length(kk);kk=kk(nk:-1:1);
	for sk=1:nk
	par1=par1([1:kk(sk)-1,kk(sk)+1:length(par1)]);
	end
end

ms1=modstruc(as,bs,cs,ds,ks,x0s);

