cvpartition — Create cross-validation partition objects.

cvpartition creates a cvpartition object for K-fold, holdout, leave-one-out, resubstitution, or custom cross-validation masks. Use training and test to extract logical masks.

Syntax

c = cvpartition(n, 'KFold', k)
c = cvpartition(stratvar, 'KFold', k, 'Stratify', tf)
c = cvpartition(n, 'Holdout', p)
c = cvpartition(n, 'Leaveout')
c = cvpartition('CustomPartition', testSets)

Inputs

NameTypeRequiredDefaultDescription
nOrStratvarAnyYesObservation count or stratification variable.
kindStringScalarYesPartition kind such as KFold, Holdout, Leaveout, Resubstitution, or CustomPartition.
valueAnyNoPartition parameter such as fold count, holdout fraction, holdout count, or custom test sets.
optionsAnyVariadicName-value options including Stratify.

Returns

NameTypeDescription
cAnyCross-validation partition object.

Errors

IdentifierWhenMessage
RunMat:cvpartition:InvalidArgumentInputs, partition kinds, dimensions, indices, or name-value options are malformed.cvpartition: invalid argument
RunMat:cvpartition:InternalRunMat cannot allocate or construct a partition output.cvpartition: internal error

How cvpartition works

  • cvpartition(n,'KFold',k) creates k deterministic round-robin folds for n observations, with k in the interval [2,n).
  • cvpartition(stratvar,'KFold',k) and cvpartition(stratvar,'Holdout',p) stratify by default. Use 'Stratify',false to partition only by observation order.
  • cvpartition(n,'Holdout',p) accepts a fraction in (0,1) or an integer count in [1,n).
  • cvpartition(n,'Leaveout') creates one test set per observation.
  • cvpartition(n,'Resubstitution') creates a single partition with all observations in training and none in test.
  • cvpartition('CustomPartition',testSets) accepts a logical vector or matrix whose columns are test sets, or a positive-integer vector whose values identify test-set numbers.
  • Numeric stratification labels containing NaN are treated as missing; those rows remain in NumObservations but are false in returned training and test masks.
  • The returned object exposes NumObservations, NumTestSets, TestSize, TrainSize, Type, IsCustom, IsGrouped, and IsStratified properties.
  • Fold assignment is deterministic for reproducibility; it does not attempt to reproduce MATLAB's RNG-dependent exact fold order.

Examples

Create K-fold masks

c = cvpartition(6, 'KFold', 3);
idxTest = test(c, 1);
idxTrainAll = training(c, 'all')

Expected output:

`idxTest` is a 6-by-1 logical vector. `idxTrainAll` is a 6-by-3 logical matrix.

Create a stratified holdout partition

group = [1; 1; 2; 2];
c = cvpartition(group, 'Holdout', 0.5);
idx = test(c)

Expected output:

`idx` selects one observation from each group.

Use custom test sets

c = cvpartition('CustomPartition', logical([1 0; 0 1; 0 0]));
idx = test(c, 'all')

Expected output:

`idx` preserves the supplied test-set columns.

Using cvpartition with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how cvpartition changes the result.

Run a small cvpartition example, explain the result, then change one input and compare the output.

FAQ

Does RunMat randomize partitions?

No. RunMat uses deterministic fold and holdout assignment so results are reproducible across runs.

How do I get masks for all folds?

Use test(c,'all') or training(c,'all'); columns correspond to partition test sets.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cvpartition is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.