When I use codegen, I get an error: Unable to delete elements from this array because dimension 2 has fixed size

1 次查看(过去 30 天)
I am following a guide for generate c/c++ code from Code Generation and Classification Learner App.
The guide give me the descrption at Define Entry-Point Function:
An entry-point function is a function you define for code generation. Because you cannot call any function at the top level using codegen, you must define an entry-point function that calls code-generation-enabled functions, and then generate C/C++ code for the entry-point function by using codegen.
In your current folder, define a function named mypredictCL.m that:
  • Accepts a numeric matrix (X) of raw observations containing the same predictor variables as the ones passed into Classification Learner
  • Loads the classification model in ClassificationLearnerModel.mat and the model parameters in ModelParameters.mat
  • Removes the predictor variables corresponding to the indices in removeVars
  • Transforms the remaining predictor data using the PCA centers (pcaCenters) and coefficients (pcaCoefficients) estimated by Classification Learner
  • Returns predicted labels using the model
Now I have written an entry-point function as:
function label = mypredictCL(X)
mdl = loadLearnerForCoder('ClassificationLearnerModel.mat'); % load the Classification model
args = coder.load('ModelParameters.mat'); % load the parameter
X(:,args.removeVars) = []; % remove the variables as the guide says
X = bsxfun(@minus,X,args.pcaCenters)*args.pcaCoefficients; preprocess the data
label = predict(mdl,X);
end
And then, I run the code in command window:
p = size(creditrating,2) - 1;
x = coder.typeof(0,[Inf,p],[1 0]);
codegen mypredictCL -args x
I get the error: Unable to delete elements from this array because dimension 2 has fixed size 7.
I want to know what is the right way to remove the variables in entry-point function?

回答(1 个)

Sakshi Sharma
Sakshi Sharma 2023-11-7
You can modify and write your entry point function in this way:
function label = mypredictCL(X)
mdl = loadLearnerForCoder('ClassificationLearnerModel.mat'); % load the Classification model
args = coder.load('ModelParameters.mat'); % load the parameter
y = X;
y(:,args.removeVars) = []; % remove the variables as the guide says
y = bsxfun(@minus,y,args.pcaCenters)*args.pcaCoefficients; %preprocess the data
label = predict(mdl,y);
end
X is defined at compile time and has fixed dimensions, so it cannot be modified inside the code.

类别

Help CenterFile Exchange 中查找有关 Code Generation 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by