MEX file that generates a subsystem (submodel) from a fuzzy model.
MATLAB help:
% FUZfuzsubsystem Generates a fuzsubsystem (submodel) from a fuzzy model.
%
% SubFIS = fuzfuzsubsystem(FIS_Model,Outputs,Rules)
%
% Arguments:
%
% FIS_Model -> Original fuzzy model.
%
% Outputs -> List of outputs for selected rules.
%
% Rules -> Selected rules (for each output) to make the fuzsubsystem.
%
% SubFIS -> Generated fuzsubsystem.
%
% For example:
%
% P = readfis('Plant.fis')
% subP = fuzfuzsubsystem(P,[1,2,2],[1,1,2])
%
% 'subP' has 3 rules, the 1st rule for the 1st output of P, and the 1st and
% 2nd rule for the 2nd output of P.
%
% See also activation, antec2mat, aproxjac, aproxlinear, conseq2mat, fis2txt,
% fuz2mat, fuzcomb, fuzeval, fuzjac, fuzlinear, fuzprint, mat2antec,
% mat2conseq, mat2fuz, txt2fis
Source code:
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
size_t i,j,n1,n2,m1,m2,n;
size_t *outputs,*rules;
size_t nOutputs, nRules;
double *poutput,*prule;
if(nrhs!=3)
{
sprintf(error,"%s %s, %s",E_NumberArg,O_OfIn,U_SeeHelp);
}
if (nlhs>1)
{
sprintf(error,"%s %s, %s",E_NumberArg,O_OfOut,U_SeeHelp);
}
for (int i=0;i<nrhs;i++)
if (mxIsEmpty(prhs[i]) || mxIsNaN(*mxGetPr(prhs[i])))
n1 = mxGetN(prhs[1]);
n2 = mxGetN(prhs[2]);
m1 = mxGetM(prhs[1]);
m2 = mxGetM(prhs[2]);
if (n1!=n2 || m1!=m2)
{
sprintf(error,"%s The sizes of Outputs and Rules must be the same, %s",E_NumberArgIn,U_SeeHelp);
}
if (n1==1 && n2==1 && m1==m2)
n=m1;
else if (m1==1 && m2==1 && n1==n2)
n=n1;
else
{
sprintf(error,"%s, %s",E_NumberArgIn,U_SeeHelp);
}
poutput = mxGetPr(prhs[1]);
prule = mxGetPr(prhs[2]);
outputs = new size_t[n];
rules = new size_t[n];
for (i=0;i<n;i++)
{
*(outputs+i) = ((size_t)*poutput)-1;
*(rules+i) = ((size_t)*prule)-1;
poutput++;
prule++;
}
{
delete []outputs;
delete []rules;
sprintf(error,"%s, %s\nAt least, 1 rule for each output is needed.\nCheck indices.",E_NumberArgIn,U_SeeHelp);
}
delete [] outputs;
delete [] rules;
const char* names[]={"name","type","andMethod","orMethod","defuzzMethod","impMethod","aggMethod","input","output","rule"};
mwSize dim[] = {1,1};
plhs[0] = mxCreateStructArray(2,dim,10,names);
{
mxDestroyArray(plhs[0]);
}
}