You are here : matlabSignal Processingcorrcoef

corrcoef() - Signal Processing

exampleR = corrcoef(A) returns
the matrix of correlation
coefficients for A, where the columns of A represent
random variables and the rows represent observations.
exampleR = corrcoef(A,B) returns
coefficients between two random variables A and B.

example[R,P] =
corrcoef(___) returns the matrix of correlation
coefficients and the matrix of p-values for 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.

example___ = corrcoef(___,Name,Value) returns
any of the output arguments from the previous syntaxes 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.


Syntax

R = corrcoef(A) exampleR = corrcoef(A,B) example[R,P] =
corrcoef(___) example[R,P,RL,RU]
= corrcoef(___) example___ = corrcoef(___,Name,Value) example


Example

Random Columns of MatrixOpen This ExampleCompute the correlation coefficients for a matrix with two normally distributed, random columns and one column that is defined in terms of another. Since the third column of A is a multiple of the second, these two variables are directly correlated, thus the correlation coefficient in the (2,3) and (3,2) entries of R is 1.x = randn(6,1);
y = randn(6,1);
A = [x y 2*y+3];
R = corrcoef(A)

R =

    1.0000   -0.6237   -0.6237
   -0.6237    1.0000    1.0000
   -0.6237    1.0000    1.0000

Two Random VariablesOpen This ExampleCompute the correlation coefficient matrix between two normally distributed, random vectors of 10 observations each.A = randn(10,1);
B = randn(10,1);
R = corrcoef(A,B)

R =

    1.0000    0.4518
    0.4518    1.0000

P-Values of MatrixOpen This ExampleCompute the correlation coefficients and p-values of a normally distributed, random matrix, with an added fourth column equal to the sum of the other three columns. Since the last column of A is a linear combination of the others, a correlation is introduced between the fourth variable and each of the other three variables.  Therefore, the fourth row and fourth column of P contain very small p-values, identifying them as significant correlations.A = randn(50,3);
A(:,4) = sum(A,2);
[R,P] = corrcoef(A)

R =

    1.0000    0.1135    0.0879    0.7314
    0.1135    1.0000   -0.1451    0.5082
    0.0879   -0.1451    1.0000    0.5199
    0.7314    0.5082    0.5199    1.0000


P =

    1.0000    0.4325    0.5438    0.0000
    0.4325    1.0000    0.3146    0.0002
    0.5438    0.3146    1.0000    0.0001
    0.0000    0.0002    0.0001    1.0000

Correlation BoundsOpen This ExampleCreate a normally distributed, random matrix, with an added fourth column equal to the sum of the other three columns, and compute the correlation coefficients, p-values, and lower and upper bounds on the coeffcients.A = randn(50,3);
A(:,4) = sum(A,2);
[R,P,RL,RU] = corrcoef(A)

R =

    1.0000    0.1135    0.0879    0.7314
    0.1135    1.0000   -0.1451    0.5082
    0.0879   -0.1451    1.0000    0.5199
    0.7314    0.5082    0.5199    1.0000


P =

    1.0000    0.4325    0.5438    0.0000
    0.4325    1.0000    0.3146    0.0002
    0.5438    0.3146    1.0000    0.0001
    0.0000    0.0002    0.0001    1.0000


RL =

    1.0000   -0.1702   -0.1952    0.5688
   -0.1702    1.0000   -0.4070    0.2677
   -0.1952   -0.4070    1.0000    0.2825
    0.5688    0.2677    0.2825    1.0000


RU =

    1.0000    0.3799    0.3575    0.8389
    0.3799    1.0000    0.1388    0.6890
    0.3575    0.1388    1.0000    0.6974
    0.8389    0.6890    0.6974    1.0000

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 alpha equal to 0.01 to compute a 99% confidence interval, which is reflected in the bounds RL and RU. The intervals defined by the coefficent 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 =

    1.0000    0.1135    0.0879    0.7314
    0.1135    1.0000   -0.1451    0.5082
    0.0879   -0.1451    1.0000    0.5199
    0.7314    0.5082    0.5199    1.0000


P =

    1.0000    0.4325    0.5438    0.0000
    0.4325    1.0000    0.3146    0.0002
    0.5438    0.3146    1.0000    0.0001
    0.0000    0.0002    0.0001    1.0000


RL =

    1.0000   -0.2559   -0.2799    0.5049
   -0.2559    1.0000   -0.4792    0.1825
   -0.2799   -0.4792    1.0000    0.1979
    0.5049    0.1825    0.1979    1.0000


RU =

    1.0000    0.4540    0.4332    0.8636
    0.4540    1.0000    0.2256    0.7334
    0.4332    0.2256    1.0000    0.7407
    0.8636    0.7334    0.7407    1.0000

NaN ValuesOpen This ExampleCreate a normally distributed matrix involving NaN values, and compute the correlation coefficient matrix, excluding any rows that contain NaN.A = randn(5,3);
A(1,3) = NaN;
A(3,2) = NaN;
A

A =

    0.5377   -1.3077       NaN
    1.8339   -0.4336    3.0349
   -2.2588       NaN    0.7254
    0.8622    3.5784   -0.0631
    0.3188    2.7694    0.7147

R = corrcoef(A,'rows','complete')

R =

    1.0000   -0.8506    0.8222
   -0.8506    1.0000   -0.9987
    0.8222   -0.9987    1.0000

Use 'all' to include all NaN values in the calculation.R = corrcoef(A,'rows','all')

R =

     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 omited.R = corrcoef(A,'rows','pairwise')

R =

    1.0000   -0.3388    0.4649
   -0.3388    1.0000   -0.9987
    0.4649   -0.9987    1.0000


Output / Return Value


Limitations


Alternatives / See Also


Reference