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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
nOrStratvar | Any | Yes | — | Observation count or stratification variable. |
kind | StringScalar | Yes | — | Partition kind such as KFold, Holdout, Leaveout, Resubstitution, or CustomPartition. |
value | Any | No | — | Partition parameter such as fold count, holdout fraction, holdout count, or custom test sets. |
options | Any | Variadic | — | Name-value options including Stratify. |
Returns
| Name | Type | Description |
|---|---|---|
c | Any | Cross-validation partition object. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cvpartition:InvalidArgument | Inputs, partition kinds, dimensions, indices, or name-value options are malformed. | cvpartition: invalid argument |
RunMat:cvpartition:Internal | RunMat cannot allocate or construct a partition output. | cvpartition: internal error |
How cvpartition works
cvpartition(n,'KFold',k)createskdeterministic round-robin folds fornobservations, withkin the interval[2,n).cvpartition(stratvar,'KFold',k)andcvpartition(stratvar,'Holdout',p)stratify by default. Use'Stratify',falseto 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
NaNare treated as missing; those rows remain inNumObservationsbut are false in returned training and test masks. - The returned object exposes
NumObservations,NumTestSets,TestSize,TrainSize,Type,IsCustom,IsGrouped, andIsStratifiedproperties. - 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.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cvpartition is executed, line by line, in Rust.
- View the source for cvpartition in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.