corrcoef — Compute Pearson correlation coefficients, p-values, and confidence bounds.
R = corrcoef(A) correlates columns of A; corrcoef(A,B) correlates the vectorized arrays as two variables. Real inputs can also return [R,P,RL,RU].
Syntax
R = corrcoef(A)
R = corrcoef(A, B)
[R, P] = corrcoef(A)
[R, P] = corrcoef(A, B)
[R, P, RL, RU] = corrcoef(A)
[R, P, RL, RU] = corrcoef(A, B)
___ = corrcoef(A, Name, Value)
___ = corrcoef(A, B, Name, Value)Inputs
A- Type
Any- Required
- Yes
- Description
- Input observations (rows are observations, columns are variables).
B- Type
Any- Required
- Yes
- Description
- Second array with the same size as A; both arrays are vectorized.
nameValuePairs- Type
Any- Required
- Variadic
- Description
- Alpha and Rows name-value arguments.
Returns
R- Type
NumericArray- Description
- Correlation coefficient matrix.
P- Type
NumericArray- Description
- P-values for testing zero correlation.
RL- Type
NumericArray- Description
- Lower confidence bounds for the correlation coefficients.
RU- Type
NumericArray- Description
- Upper confidence bounds for the correlation coefficients.
Returned values from corrcoef depend on how many outputs the caller requests.
Errors
RunMat:corrcoef:InvalidArgument- When
- Arguments are malformed or unsupported for corrcoef.
- Message
corrcoef: invalid argument
RunMat:corrcoef:ComplexOutputs- When
- P-values or confidence bounds are requested for complex correlation coefficients.
- Message
corrcoef: P-values and confidence bounds are invalid for complex coefficients
RunMat:corrcoef:SizeMismatch- When
- A and B do not have the same size.
- Message
corrcoef: A and B must have the same size
Inputs and outputs
Rows are observations and columns are variables for a single matrix. Two same-sized arrays are vectorized into a two-variable problem; they are not concatenated by column as separate multivariate matrices.
For real data, [R,P,RL,RU] returns correlation coefficients, p-values for zero correlation, and lower/upper confidence bounds. Alpha sets the significance level (strictly between zero and one). Complex data supports the correlation matrix but not the statistical outputs.
Constant variables have undefined correlations, represented by NaN.
Missing values and execution
Rows accepts all, complete, or pairwise. Complete removes observations missing from any variable; pairwise filters separately for each pair. The default is all.
Real typed-integer and logical observations are RunMat extensions and return double results. RunMat returns single numerical outputs for single observations and double if any observation data are double, integer, or logical; Alpha does not promote the output. The host calculation may accumulate in double precision. A supported provider may retain a GPU-resident result for Rows=all and one output. Other admitted forms gather resident inputs and return a dense host result from the reference path; unlike cov, this fallback does not restore device placement. If a completed provider correlation needs a class conversion rejected as unsupported before conversion effects, RunMat materializes and converts that produced result without recomputing correlation. A provider choice or output residency is not promised statically.
The call is a fusion boundary. An upstream expression can already have a resident result, so an explicit gpuArray call is not required solely to make a supported correlation eligible for the provider hook. An explicit gpuArray remains useful when device residency is intended. The resident hook requires all observation inputs on the same provider, Rows=all, one output, supported real precision, and the provider's correlation operation; otherwise the admitted host path returns a dense host result. Its portable runtime binding applies in interpreted, JIT, native compiled, and browser execution; GPU availability is provider-specific.
Examples
Correlate matrix columns
A = [1 2 4; 2 4 1; 3 6 -1; 4 8 0]; R = corrcoef(A)Expected output:
R = [1 1 -0.8367; 1 1 -0.8367; -0.8367 -0.8367 1] (rounded to four decimals)Correlate two arrays
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 0.9998; 0.9998 1] (rounded to four decimals)Omit incomplete observations
X = [1 NaN 2; 3 4 5; 6 7 NaN]; R = corrcoef(X,'Rows','complete')Expected output:
One complete observation cannot establish a correlation.Filter separately for each variable pair
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 1]Request statistical outputs
X = [1 2; 2 1; 3 3; 4 5; 5 4]; [R,P,RL,RU] = corrcoef(X,'Alpha',0.1)Expected output:
R, P, RL, and RU are all 2-by-2 matrices.Correlate resident data
G = gpuArray([1 2 4; 2 4 1; 3 6 -1; 4 8 0]); R = corrcoef(G); R_host = gather(R)Expected output:
R_host is a 3-by-3 correlation matrix.Preserve single precision across statistical outputs
X = single([1 2; 2 3; 3 1; 4 5]); [R,P,RL,RU] = corrcoef(X,'Alpha',0.1)Expected output:
All four outputs have the single class.Using corrcoef with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how corrcoef changes the result.
Run a small corrcoef example, explain the result, then change one input and compare the output.
FAQ
Can corrcoef return p-values?⌄
Yes. Real observations can return P and optional confidence bounds RL and RU. Complex observations support only R.
Does corrcoef have a normalization flag?⌄
No. Correlations are normalized by their standard deviations; corrcoef(A,1) is not a normalization form. Use Rows and Alpha options where appropriate.
How are constant columns handled?⌄
Correlations involving zero-variance columns are NaN.
Which observations do complete and pairwise use?⌄
Complete keeps only rows finite in every variable; pairwise chooses finite rows separately for each variable pair.
Can logical and typed-integer observations be used?⌄
Yes in RunMat compatibility mode. They are converted at the statistical calculation boundary and produce double host outputs.
How does corrcoef differ from cov?⌄
Covariance remains in the variables' squared units; correlation divides covariance by the product of standard deviations.
Can I correlate two vectors?⌄
Yes. corrcoef(x,y) vectorizes equally sized inputs into two variables and returns a 2-by-2 matrix; the off-diagonal value is their Pearson correlation. Paired matrices are also vectorized, not concatenated column-wise as independent variables.
How do I omit missing observations?⌄
Use Rows='complete' to remove an observation containing NaN or Inf in any variable, or Rows='pairwise' to select finite observations separately for each variable pair.
Related Stats functions
Summary
anova1 · binocdf · binscatter · boxplot · cdf · cdfplot · chi2cdf · corr · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · ksdensity · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pca · pdf · prctile · qqplot · quantile · refline · rmse · scatterhist · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest · ttest2 · wblinv
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · nlparci · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · gpurng · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Source you can read
Every RunMat function's source is public. Read exactly how corrcoef is executed, line by line, in Rust.
- View the source for corrcoef in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.