MATLAB Answers

help with error in my code

92 views (last 30 days)
Ditf on 31 Dec 2020
Edited: Stephen Cobeldick on 2 Jan 2021
This question was flagged by Walter Roberson
Solved, thank you for the help
apologies for deleting my question and code, however multiple people in my class are working on the same project and this is a crucial part of it, so i did not want any one to copy my code.
i truly appreciate all the help.
Stephen Cobeldick
Stephen Cobeldick on 2 Jan 2021
Original question by Dilpreet kaur retrieved from Google Cache:
help with error in my code
Hi can someone help me understand the mistake in my code, i followed the correct syntax from
i keep getting the error
operator "==" not supported for operands of type "cvpartition"
error in line 24
test = (indices == 1)
k = 4;
n = 699; %sample lenght
rng ('default')
indices = cvpartition(n,'kfold', k);
for i = 1:k
test= (indices == i); train = ~test;
class = classify(InputVariable(test,:),InputVariable(train,:),OutputVariable(train,:));
plotconfusion(testTarget, testY)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 31 Dec 2020
I get this:
k = 4;
n = 699; %sample lenght
rng ('default')
indices = cvpartition(n,'kfold', k)
indices =
K-fold cross validation partition
NumObservations: 699
NumTestSets: 4
TrainSize: 525 524 524 524
TestSize: 174 175 175 175
You're not using indices correctly. It's an object, not a list of indices. If you want a listof indices, use randperm().

More Answers (1)

Walter Roberson
Walter Roberson on 1 Jan 2021
Edited: Walter Roberson on 2 Jan 2021
cvpartition does not return indices.
rng ('default')
nfold = 4;
cvfolds = cvpartition(699,'kfold', nfold);
cp = classperf(OutputVariable); % initializes the CP object
for i = 1:nfold
test = cvfolds.test(i);
train =;
class = classify(InputVariable(test,:), InputVariable(train,:), OutputVariable(train,:));
classperf(cp, class, test);

Community Treasure Hunt

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

Start Hunting!