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

3 Ansichten (letzte 30 Tage)
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?

Antworten (1)

Sakshi Sharma
Sakshi Sharma am 7 Nov. 2023
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.

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by