corrcov — Convert a covariance matrix to a correlation matrix.

corrcov(C) returns the correlation matrix corresponding to the square covariance matrix C. [R,sigma] = corrcov(C) also returns the standard deviations from sqrt(diag(C)) as an n-by-1 vector.

Syntax

R = corrcov(C)
[R, sigma] = corrcov(C)

Inputs

NameTypeRequiredDefaultDescription
CAnyYesSquare covariance matrix.

Returns

NameTypeDescription
RNumericArrayCorrelation matrix corresponding to the covariance matrix.
sigmaNumericArrayColumn vector of standard deviations computed from diag(C).

Returned values from corrcov depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:corrcov:InvalidArgumentInput is nonnumeric, complex, not single/double precision, non-square, has negative variances, violates covariance bounds, or too many arguments/outputs are supplied.covariance conversion: invalid argument
RunMat:corrcov:InternalInternal tensor allocation fails.covariance conversion: internal error

How corrcov works

  • C must be a real numeric square matrix stored as double or single precision. Scalar numeric values are accepted as 1-by-1 covariance matrices.
  • The diagonal entries of C are interpreted as variances and must be nonnegative.
  • RunMat validates symmetry within floating-point tolerance before converting.
  • RunMat rejects covariance entries whose magnitude exceeds the variance-implied pairwise bound sqrt(C(i,i)*C(j,j)).
  • Entries involving a zero-variance variable produce NaN because the correlation denominator is zero.
  • Logical inputs are promoted to double precision.
  • Real gpuArray inputs stay resident when the active provider implements covariance-to-correlation conversion.

Example

Convert covariance to correlation

C = [4 2; 2 9];
[R,sigma] = corrcov(C)

Expected output:

R = [1 0.3333; 0.3333 1]
sigma = [2; 3]

Using corrcov with coding agents

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

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

FAQ

What does the second output contain?

sigma is an n-by-1 vector of standard deviations computed from the square root of the covariance matrix diagonal.

How are constant variables handled?

A zero diagonal entry means the corresponding variable has zero variance. Correlations involving that variable return NaN, matching the undefined zero-denominator calculation.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how corrcov 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.