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
    • 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

cov — Compute covariance matrices in MATLAB and RunMat.

cov returns covariance matrices for numeric data with columns as variables and rows as observations. Single-matrix, paired-input, weighting, and row-handling forms follow MATLAB semantics.

Syntax

C = cov(X)
C = cov(X, Y_or_w)
C = cov(X, normalization)
C = cov(X, rows_option)
C = cov(X, Y, opt)
C = cov(X, Y, w)
C = cov(X, Y, w, opt)

Inputs

NameTypeRequiredDefaultDescription
XAnyYes—Input observations (rows are observations, columns are variables).
Y_or_wAnyYes—Second dataset (Y) or weight vector (w), depending on shape/position.
normalizationNumericScalarYes0Normalization flag: 0 (unbiased) or 1 (biased).
rows_optionStringScalarYes"all"Rows handling mode: 'all', 'omitrows', or 'partialrows'.
YAnyYes—Second dataset with matching size (or equal vector length).
optAnyYes—Normalization flag or rows option.
wAnyYes—Weight vector with one weight per observation row.

Returns

NameTypeDescription
CNumericArrayCovariance matrix.

Errors

IdentifierWhenMessage
RunMat:cov:InvalidArgumentArguments are malformed or unsupported for cov.cov: invalid argument
RunMat:cov:ComplexUnsupportedAny argument is complex-valued.cov: complex inputs are not supported yet
RunMat:cov:RowsMismatchTwo input datasets do not have the same size or equal vector lengths.cov: paired inputs must have the same size
RunMat:cov:NormalizationInvalidNormalization flag is non-finite, non-integer, or not 0/1.cov: normalization flag is invalid
RunMat:cov:WeightVectorLengthMismatchWeight vector length does not match observation row count.cov: weight vector length mismatch
RunMat:cov:RowsOptionUnknownRows option is not one of all/omitrows/partialrows.cov: unknown rows option
RunMat:cov:NormalizationDuplicateNormalization flag is provided more than once.cov: normalization flag specified more than once
RunMat:cov:TooManyArrayArgumentsMore than two data arrays (or Y plus weight) are provided.cov: too many array arguments
RunMat:cov:InternalInternal tensor conversion/allocation or covariance computation fails.cov: internal operation failed

How cov works

  • cov(X) treats each column of X as a variable and returns a square covariance matrix.
  • cov(X, Y) concatenates X and Y column-wise (they must have the same number of rows) before computing the covariance.
  • The second argument can be the normalization flag 0 (default) or 1, matching MATLAB's unbiased and biased estimators.
  • You can pass a weight vector to obtain frequency-weighted covariance.
  • 'omitrows' drops rows containing NaN or Inf before the covariance is computed.
  • 'partialrows' performs pairwise deletion: each covariance entry uses only the rows that contain finite values for that column pair.

Does RunMat run cov on the GPU?

RunMat invokes provider-specific GPU kernels when:

1. Data inputs already reside on the GPU; 2. The rows option is 'all'; 3. Any weight vector is either a host numeric/logical vector that can be uploaded temporarily or a real gpuArray vector on the same provider; and 4. The active provider exposes the custom covariance hook.

If any of these conditions is not met, RunMat gathers the data to the host, evaluates the reference implementation, and returns a dense host tensor. This guarantees MATLAB-compatible behaviour regardless of GPU support.

GPU memory and residency

You usually do not need to call gpuArray. Expressions such as cov(sin(X)) keep temporary results on the GPU as long as the active provider handles the operation. For rows='all', observation weight vectors can also stay on the provider path: host weights are uploaded temporarily and resident real gpuArray row or column vectors are used directly when they belong to the same provider. The builtin gathers to the CPU for 'omitrows', 'partialrows', unsupported complex/provider-mismatched resident weights, or when the provider does not implement the covariance hook. Explicitly calling gpuArray remains supported for MATLAB compatibility and to seed GPU residency when you are unsure about planner decisions.

Examples

Computing covariance of columns in a matrix

X = [4.0 2.0 0.60;
     4.2 2.1 0.59;
     3.9 2.0 0.58;
     4.3 2.1 0.62;
     4.1 2.2 0.63];
C = cov(X)

Expected output:

C =
    0.0250    0.0075    0.0018
    0.0075    0.0070    0.0014
    0.0018    0.0014    0.0004

Covariance between two vectors

x = [1 2 3 4]';
y = [10 11 9 12]';
C = cov(x, y)

Expected output:

C =
    1.6667    0.6667
    0.6667    1.6667

Weighted covariance with observation weights

X = [4.0 2.0;
     4.2 2.1;
     3.9 2.0;
     4.3 2.1;
     4.1 2.2];
w = [1 1 1 2 2];
Cw = cov(X, w)

Expected output:

Cw =
    0.0224    0.0050
    0.0050    0.0067

Ignoring rows that contain missing values

X = [1   NaN 2;
     3   4   5;
     NaN 6   7;
     8   9   10];
C = cov(X, 'omitrows')

Expected output:

C =
   12.5000   12.5000   12.5000
   12.5000   12.5000   12.5000
   12.5000   12.5000   12.5000

Pairwise covariance with staggered NaNs

X = [ 1   2   NaN;
      4   NaN 6;
      7   8   9];
C = cov(X, 'partialrows')

Expected output:

C =
    9.0000   18.0000    4.5000
   18.0000   18.0000       NaN
    4.5000       NaN    4.5000

Running covariance on gpuArray inputs

X = [4.0 2.0 0.60;
     4.2 2.1 0.59;
     3.9 2.0 0.58;
     4.3 2.1 0.62;
     4.1 2.2 0.63];
G = gpuArray(X);
CG = cov(G);
CG_host = gather(CG)

Expected output:

CG_host =
    0.0250    0.0075    0.0018
    0.0075    0.0070    0.0014
    0.0018    0.0014    0.0004

Using cov with coding agents

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

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

FAQ

Does cov support biased and unbiased estimators?⌄

Yes. The default is the unbiased estimator (divide by *N - 1*). Passing 1 as the second argument switches to the biased estimator (divide by *N*), matching MATLAB.

How do I provide observation weights?⌄

Supply a weight vector whose length equals the number of observations. The covariance is frequency-weighted using the MATLAB formula. With gpuArray data and rows='all', RunMat keeps the result resident when the active provider supports weighted covariance; row-filtering modes fall back to the CPU implementation.

What happens when columns contain constant values?⌄

The diagonal entries become zero, and off-diagonal entries involving the constant column are zero. Any slight negative values caused by floating-point noise are clamped to zero.

How are NaN and Inf handled?⌄

By default ('all'), non-finite values propagate NaN into the affected covariance entries. 'omitrows' drops rows containing non-finite values, while 'partialrows' recomputes each covariance entry using only rows that are finite for the relevant column pair.

Can I call cov on logical inputs?⌄

Yes. Logical arrays are converted to double precision (true → 1.0, false → 0.0) before the covariance is computed, matching MATLAB's behaviour.

Related Stats functions

Summary

binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · 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

Ml

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

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 cov is executed, line by line, in Rust.

  • View the source for cov 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 cov works
  • Does RunMat run cov on the GPU?
  • GPU memory and residency
  • Examples
  • Computing covariance of columns in a matrix
  • Covariance between two vectors
  • Weighted covariance with observation weights
  • Ignoring rows that contain missing values
  • Pairwise covariance with staggered NaNs
  • Running covariance on gpuArray inputs
  • Using cov with coding agents
  • FAQ
  • Related Stats functions
  • Summary
  • Ml
  • Random
  • Hist
  • Options
  • Open-source implementation
  • About RunMat