corrcoef — Compute Pearson correlation coefficients for the columns of matrices or paired data sets.
corrcoef returns the Pearson correlation coefficients between pairs of variables. Given a matrix, every column is treated as a separate variable and every row is an observation. You can also pass two matching data sets and RunMat will concatenate them column-wise, mirroring MATLAB.
How corrcoef works in RunMat
corrcoef(X)returns a square matrix whose(i, j)entry is the correlation between columniand columnjofX.corrcoef(X, Y)concatenates the columns ofXandY(they must share the same number of rows) before computing correlations.- An optional normalisation flag allows you to divide by
N - 1(default, unbiased) orN(flag = 1, biased) just like MATLAB. - Use
'rows'with'all','complete', or'pairwise'to control how rows containingNaNorInfare handled. - Columns with zero variance produce
NaNon both the diagonal and in every correlation that references them, matching MATLAB semantics.
How corrcoef runs on the GPU
When the input data resides on the GPU, RunMat asks the active acceleration provider to execute the correlation directly on the device whenever:
1. All inputs are gpuArray values; 2. The 'rows' option is 'all'; and 3. The provider exposes the custom corrcoef hook (the WGPU provider does).
If any of these conditions is not met, the builtin gathers the data to host memory, computes the correlation matrix on the CPU reference path, and returns a dense host tensor.
GPU memory and residency
You usually do **not** need to call gpuArray explicitly. When you write expressions such as corrcoef(sin(X)), the fusion planner keeps the intermediate residency on the GPU as long as the active provider exposes the required hooks (for corrcoef, that means the custom provider kernel and rows='all'). RunMat still honours MATLAB semantics, so explicitly calling gpuArray remains useful for compatibility and for forcing GPU residency when you are unsure whether the planner will do so.
Examples
Calculating correlation between matrix columns
A = [1 2 4; 2 4 1; 3 6 -1; 4 8 0];
R = corrcoef(A)Expected output:
R =
1.0000 1.0000 -0.8367
1.0000 1.0000 -0.8367
-0.8367 -0.8367 1.0000Correlating two separate data sets
height = [1.72 1.84 1.65 1.91]';
weight = [68.5 83.0 59.1 92.2]';
R = corrcoef(height, weight)Expected output:
R =
1.0000 0.9998
0.9998 1.0000Ignoring rows that contain missing values
X = [1 NaN 2;
3 4 5;
6 7 NaN];
R = corrcoef(X, 'rows', 'complete')Expected output:
R =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaNPairwise correlation with staggered NaNs
X = [ 1 2 3;
NaN 5 1;
4 NaN 6;
5 8 NaN];
R = corrcoef(X, 'rows', 'pairwise')Expected output:
R =
1 1 1
1 1 -1
1 -1 1Using biased normalisation (flag = 1)
A = [1 2; 3 4; 5 6];
R = corrcoef(A, 1)Expected output:
R =
1.0000 1.0000
1.0000 1.0000Running corrcoef on gpuArray inputs
G = gpuArray([1 2 4; 2 4 1; 3 6 -1; 4 8 0]);
R = corrcoef(G);
R_host = gather(R)FAQ
Does corrcoef support two outputs like MATLAB?
RunMat currently returns the correlation matrix (R) and omits the optional p-value output. The statistical distribution helpers needed for the p-values are on the roadmap.
How does the normalisation flag affect the result?
flag = 0 (default) divides by N - 1 for an unbiased estimate. flag = 1 divides by N, producing biased estimates that match MATLAB. The choice does not change perfect correlations because both variance and covariance scale by the same factor.
What happens when a column is constant?
Columns with zero variance produce NaN on the diagonal and in any correlation that references that column. MATLAB behaves the same way because the standard deviation is zero.
Which rows are removed by 'rows','complete'?
All rows that contain any NaN or Inf values are discarded before computing the correlation matrix. 'rows','pairwise' performs this filtering separately for each column pair.
Can I call corrcoef on logical arrays?
Yes. Logical inputs are promoted to double precision (true -> 1.0, false -> 0.0) before the correlation matrix is computed.
What does corrcoef do in MATLAB?
corrcoef(X) returns the matrix of correlation coefficients for the columns of X. Each entry R(i,j) is the Pearson linear correlation between columns i and j, ranging from -1 to 1.
How is corrcoef different from cov in MATLAB?
corrcoef returns normalized correlation coefficients (always between -1 and 1), while cov returns the covariance matrix (in the original units squared). corrcoef(X) equals cov(X) ./ (std(X)' * std(X)).
Can I compute the correlation between two vectors with corrcoef?
Yes. corrcoef(x, y) treats x and y as two columns and returns a 2×2 correlation matrix. The off-diagonal element is the correlation coefficient between x and y.
Does corrcoef handle missing data?
Use corrcoef(X, 'rows', 'complete') to exclude any row that contains a NaN in any column, or 'pairwise' to exclude NaN rows separately for each pair of columns.
Related functions to explore
These functions work well alongside corrcoef. Each page has runnable examples you can try in the browser.
mean, sum, histcounts, gpuArray, gather, cov
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how corrcoef works, line by line, in Rust.
- View corrcoef.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.