Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
corrcoef
Correlation coefficients
Syntax
R = corrcoef(A)
R = corrcoef(A,B)
Description
R = corrcoef(A) returns the matrix of correlation coefficients for A, where the columns of A represent example
random variables and the rows represent observations.
example
R = corrcoef(A,B) returns coefficients between two random variables A and B.
[R,P] = corrcoef( ___ ) returns the matrix of correlation coefficients and the matrix of p-values for example
testing the hypothesis that there is no relationship between the observed phenomena (null hypothesis).
Use this syntax with any of the arguments from the previous syntaxes. If an off-diagonal element of P is
smaller than the significance level (default is 0.05), then the corresponding correlation in R is considered
significant. This syntax is invalid if R contains complex elements.
example
[R,P,RL,RU] = corrcoef( ___ ) includes matrices containing lower and upper bounds for a 95%
confidence interval for each coefficient. This syntax is invalid if R contains complex elements.
___ = corrcoef( ___ ,Name,Value) returns any of the output arguments from the previous syntaxes example
with additional options specified by one or more Name,Value pair arguments. For example,
corrcoef(A,'Alpha',0.1) specifies a 90% confidence interval, and
corrcoef(A,'Rows','complete') omits all rows of A containing one or more NaN values.
x = randn(6,1);
y = randn(6,1);
A = [x y 2*y+3];
R = corrcoef(A)
R = 3×3
https://www.mathworks.com/help/matlab/ref/corrcoef.html 1/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
A = randn(10,1);
B = randn(10,1);
R = corrcoef(A,B)
R = 2×2
1.0000 0.4518
0.4518 1.0000
P-Values of Matrix
A = randn(50,3);
A(:,4) = sum(A,2);
[R,P] = corrcoef(A)
R = 4×4
P = 4×4
Correlation Bounds
https://www.mathworks.com/help/matlab/ref/corrcoef.html 2/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
Create a normally distributed, random matrix, with an added fourth View MATLAB Command
column equal to the sum of the other three columns, and compute the
correlation coefficients, p-values, and lower and upper bounds on the
coefficients.
A = randn(50,3);
A(:,4) = sum(A,2);
[R,P,RL,RU] = corrcoef(A)
R = 4×4
P = 4×4
RL = 4×4
RU = 4×4
The matrices RL and RU give lower and upper bounds, respectively, on each correlation coefficient according to a
95% confidence interval by default. You can change the confidence level by specifying the value of Alpha, which
defines the percent confidence, 100*(1-Alpha)%. For example, use an Alpha value equal to 0.01 to compute a
99% confidence interval, which is reflected in the bounds RL and RU. The intervals defined by the coefficient
bounds in RL and RU are bigger for 99% confidence compared to 95%, since higher confidence requires a more
inclusive range of potential correlation values.
[R,P,RL,RU] = corrcoef(A,'Alpha',0.01)
R = 4×4
P = 4×4
https://www.mathworks.com/help/matlab/ref/corrcoef.html 3/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
RL = 4×4
RU = 4×4
NaN Values
A = randn(5,3);
A(1,3) = NaN;
A(3,2) = NaN;
A
A = 5×3
R = corrcoef(A,'Rows','complete')
R = 3×3
R = corrcoef(A,'Rows','all')
R = 3×3
https://www.mathworks.com/help/matlab/ref/corrcoef.html 4/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
1 NaN NaN
NaN NaN NaN
NaN NaN NaN
Use 'pairwise' to compute each two-column correlation coefficient on a pairwise basis. If one of the two
columns contains a NaN, that row is omitted.
R = corrcoef(A,'Rows','pairwise')
R = 3×3
A — Input array
matrix
Example: R = corrcoef(A,'Alpha',0.03)
Significance level, specified as a number between 0 and 1. The value of the 'Alpha' parameter defines the
percent confidence level, 100*(1-Alpha)%, for the correlation coefficients, which determines the bounds in RL and
RU.
• 'all' — Include all NaN values in the input before computing the correlation coefficients.
• 'complete' — Omit any rows of the input containing NaN values before computing the correlation
coefficients. This option always returns a positive semi-definite matrix.
• 'pairwise' — Omit any rows containing NaN only on a pairwise basis for each two-column correlation
coefficient calculation. This option can return a matrix that is not positive semi-definite.
R — Correlation coefficients
matrix
• For one matrix input, R has size [size(A,2) size(A,2)] based on the number of random variables
(columns) represented by A. The diagonal entries are set to one by convention, while the off-diagonal entries
are correlation coefficients of variable pairs. The values of the coefficients can range from -1 to 1, with -1
representing a direct, negative correlation, 0 representing no correlation, and 1 representing a direct, positive
correlation. R is symmetric.
• For two input arguments, R is a 2-by-2 matrix with ones along the diagonal and the correlation coefficients
along the off-diagonal.
• If any random variable is constant, its correlation with all other variables is undefined, and the respective row
and column value is NaN.
P — P-values
matrix
https://www.mathworks.com/help/matlab/ref/corrcoef.html 6/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
P-values, returned as a matrix. P is symmetric and is the same size as R. The diagonal entries are all ones and the
off-diagonal entries are the p-values for each variable pair. P-values range from 0 to 1, where values close to 0
correspond to a significant correlation in R and a low probability of observing the null hypothesis.
Lower bound for correlation coefficient, returned as a matrix. RL is symmetric and is the same size as R. The
diagonal entries are all ones and the off-diagonal entries are the 95% confidence interval lower bound for the
corresponding coefficient in R. The syntax returning RL is invalid if R contains complex values.
Upper bound for correlation coefficient, returned as a matrix. RU is symmetric and is the same size as R. The
diagonal entries are all ones and the off-diagonal entries are the 95% confidence interval upper bound for the
corresponding coefficient in R. The syntax returning RL is invalid if R contains complex values.
Correlation Coefficient
The correlation coefficient of two random variables is a measure of their linear dependence. If each variable has N
scalar observations, then the Pearson correlation coefficient is defined as
(‾A‾‾‾
N
−‾μ‾‾
)(
B − μB
)
1
(
ρ A, B )= i A i
,
N − 1 σA σB
i=1
where μ and σ are the mean and standard deviation of A, respectively, and μ and σ are the mean and
A A B B
standard deviation of B. Alternatively, you can define the correlation coefficient in terms of the covariance of A and
B:
(
ρ A, B ) = cov( A, B) .
σ Aσ B
The correlation coefficient matrix of two random variables is the matrix of correlation coefficients for each pairwise
variable combination,
( )
(
ρ A, A ) (
ρ A, B )
R = .
ρ( B, A) ρ( B, B)
Since A and B are always directly correlated to themselves, the diagonal entries are just 1, that is,
( )
1 (
ρ A, B )
R = .
(
ρ B, A ) 1
References
https://www.mathworks.com/help/matlab/ref/corrcoef.html 7/8
11/26/2019 Correlation coefficients - MATLAB corrcoef
[1] Fisher, R.A. Statistical Methods for Research Workers, 13th Ed., Hafner, 1958.
[2] Kendall, M.G. The Advanced Theory of Statistics, 4th Ed., Macmillan, 1979.
[3] Press, W.H., Teukolsky, S.A., Vetterling, W.T., and Flannery, B.P. Numerical Recipes in C, 2nd Ed., Cambridge
University Press, 1992.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing
Toolbox™.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing
Toolbox™.
See Also
cov | mean | plotmatrix | std
https://www.mathworks.com/help/matlab/ref/corrcoef.html 8/8