# Error with reshape of number of elements

2 views (last 30 days)
Fed on 25 Jan 2019
Commented: Fed on 1 Feb 2019
Hello,
I'm working with multi-class classification by using function fitcecoc.
Featureste is a 1063x22 matrix while Wte is a 1063x1 vector.
Running the code shown below, a message error is given, saying: 'To RESHAPE the number of elements must not change'
Error is given at the following line:
reshape(max(PosteriorRegion,[ ],2),size(x1Grid,1),size(x1Grid,2)))
How can I fix it? Thank you in advance
Mdl = fitcecoc(Featureste,Wte,'Learners',t,'FitPosterior',1,...
'ClassNames',{'1','2','3'},...
'Verbose',2)
%Cross-validate Mdl using 10-fold cross-validation.
CVMdl = crossval(Mdl);
%CVMdl is a ClassificationPartitionedECOC cross-validated ECOC classifier.
%Estimate the classification error.
loss = kfoldLoss(CVMdl);
%Define a grid of values in the observed predictor space.
%Predict the posterior probabilities for each instance in the grid.
xMax = max(Featureste);
xMin = min(Featureste);
x1Pts = linspace(xMin(1),xMax(1));
x2Pts = linspace(xMin(2),xMax(2));
[x1Grid,x2Grid] = meshgrid(x1Pts,x2Pts);
[~,~,~,PosteriorRegion] = predict(Mdl,[x1Grid(:),x2Grid(:)]);
%For each coordinate on the grid
%plot the maximum class posterior probability among all classes.
figure
contourf(x1Grid,x2Grid,...
reshape(max(PosteriorRegion,[],2),size(x1Grid,1),size(x1Grid,2)))
hold on
gh = gscatter(Featureste(:,1),Featureste(:,2),Wte,'krk','*xd',8)
gh(2).LineWidth = 2;
gh(3).LineWidth = 2;
hold off

Cris LaPierre on 25 Jan 2019
Here's my guess. You create x1Pts and x2Pts without specifying the number of elements you want. When you do that, you get a vector with length 100. See this comment from the documentation page:
• y = linspace(x1,x2) returns a row vector of 100 evenly spaced points between x1 and x2.
Therefore, x1Grid and x2Grid and 100x100.
I have no idea what the size of PosteriorRange is, but max(PosteriorRegion,[],2) must NOT be a 10000 x 1 (size(x1Grid,1) = size(x1Grid,2) = 100).
Fed on 1 Feb 2019
Thank you!