RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • bayesopt
    • classify
    • confusionmat
    • crossvalind
    • cvpartition
    • fitclinear
    • fitctree
    • fitlm
    • kmeans
    • knnsearch
    • lasso
    • lassoglm
    • linkage
    • lscov
    • mnrfit
    • optimizableVariable
    • pdist
    • pdist2
    • perfcurve
    • predict
    • regress
    • ridge
    • squareform
    • test
    • training
    • tsne

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
nOrStratvarAnyYes—Observation count or stratification variable.
kindStringScalarYes—Partition kind such as KFold, Holdout, Leaveout, Resubstitution, or CustomPartition.
valueAnyNo—Partition parameter such as fold count, holdout fraction, holdout count, or custom test sets.
optionsAnyVariadic—Name-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 randomly assigned balanced folds for n observations, with k in the interval [2,n), using RunMat's current runtime RNG state.
  • 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.
  • Typed-integer n, KFold or Holdout controls, stratification labels, and custom test sets are four independently gated RunMat extensions. Documented single, double, and logical forms remain accepted as appropriate to each argument.
  • The Stratify value uses documented logical scalars in compatibility mode; numeric, integer, text, and other broad boolean aliases require the cvpartition-nonlogical-stratify-option extension.
  • Random partitions respect RunMat's runtime RNG: the same seed reproduces the same masks, while different seeds can change assignment without changing fold-size invariants.
  • General compatibility gaps remain for R2025a GroupingVariables, tall-array holdout, the compatibility target's GPU stratification and custom partitions, and portions of object summary and repartition behavior.

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?⌄

Yes. K-fold and holdout assignment consumes RunMat's runtime RNG; set the seed to reproduce masks.

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

histc · histcounts · histcounts2

Options

statget · statset

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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How cvpartition works
  • Examples
  • Create K-fold masks
  • Create a stratified holdout partition
  • Use custom test sets
  • Using cvpartition with coding agents
  • FAQ
  • Related Stats functions
  • Ml
  • Summary
  • Random
  • Hist
  • Options
  • Open-source implementation
  • About RunMat