Invalid RHS for assignment to a categorical array.
17 views (last 30 days)
Show older comments
function [result] = multisvm(TrainingSet,Group_Train1,TestSet,actual_label)
%Models a given training set with a corresponding group vector and
%classifies a given test set using an SVM classifier according to a
%one vs. all relation.
%
%This code was written by Cody Neuburger cneuburg@fau.edu
%Florida Atlantic University, Florida USA...
%This code was adapted and cleaned from Anand Mishra's multisvm function
%found at http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine/
u=unique(Group_Train1);
numClasses=length(u);
result = categorical.empty();
%result = zeros(length(TestSet(:,1)),1);
%build models
models = cell(numClasses,1);
for k=1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G1vAll=(Group_Train1==u(k));
models{k} = fitcsvm(TrainingSet,G1vAll,'KernelFunction','polynomial','polynomialorder',3,'Solver','ISDA','Verbose',0,'Standardize',true);
if ~models{k}.ConvergenceInfo.Converged
fprintf('Training did not converge for class "%s"\n', string(u(k)));
end
end
%classify test cases
for t=1:size(TestSet,1)
matched = false;
for k = numClasses:-1:1
% for k =1: numClasses
if(predict(models{k},TestSet(t,: )))
matched = true;
break;
end
end
if matched
result(t,1) = u(k);
%result(t) = u(k);
else
result(t,1) = 'No Match';
%--------------------------------
end
end
%Accuracy = mean(Group_Test1==result) * 100;
%fprintf('Accuracy = %.2f\n', Accuracy);
%fprintf('error rate = %.2f\n ', mean(result ~= Group_Test1 ) * 100);
%confusionchart(Group_Test1, result);
end
>> HOG2
Error using categorical/subsasgn (line 72)
Invalid RHS for assignment to a categorical array.
Error in multisvm (line 52)
result(t,1) = u(k);
Error in HOG2 (line 30)
result= multisvm(Feat1,Group_Train1,Feat2,actual_label);
0 Comments
Accepted Answer
Jan
on 16 May 2022
Group_Train1 is a [274x1] double vector. u contains its unique elements. result is an empty categorical.
The error message means, that you cannot insert scalar doubles into an array of categoricals:
result = categorical.empty();
result(1) = 19 % FAIL
I'm confused by the different error message. Which Matlab version do you use?
More Answers (0)
See Also
Categories
Find more on Classification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!