This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

cvpartition class

Data partitions for cross validation


An object of the cvpartition class defines a random partition on a set of data of a specified size. Use this partition to define test and training sets for validating a statistical model using cross validation.


cvpartitionCreate cross validation partition for data


dispDisplay cvpartition object
displayDisplay cvpartition object
repartitionRepartition data for cross-validation
testTest indices for cross-validation
trainingTraining indices for cross-validation


NumObservationsNumber of observations (including observations with missing group values)
NumTestSetsNumber of test sets
TestSizeSize of each test set
TrainSizeSize of each training set
TypeType of partition

Copy Semantics

Value. To learn how this affects your use of the class, see Comparing Handle and Value Classes in the MATLAB® Object-Oriented Programming documentation.


Use a 10-fold stratified cross validation to compute the misclassification error for classify on iris data.

CVO = cvpartition(species,'k',10);
err = zeros(CVO.NumTestSets,1);
for i = 1:CVO.NumTestSets
    trIdx =;
    teIdx = CVO.test(i);
    ytest = classify(meas(teIdx,:),meas(trIdx,:),...
    err(i) = sum(~strcmp(ytest,species(teIdx)));
cvErr = sum(err)/sum(CVO.TestSize);

Was this topic helpful?