When I use codegen, I get an error: Unable to delete elements from this array because dimension 2 has fixed size
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Code Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!