Linear or rank correlation
RHO = corr(X)
RHO = corr(X,Y)
[RHO,PVAL] = corr(X,Y)
[RHO,PVAL] = corr(X,Y,'name',value)
RHO = corr(X) returns a p-by-p matrix containing the pairwise linear correlation coefficient between each pair of columns in the n-by-p matrix X.
RHO = corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
The difference between corr(X,Y) and the MATLAB® function corrcoef(X,Y) is that corrcoef(X,Y) returns a matrix of correlation coefficients for the two column vectors X and Y. If X and Y are not column vectors, corrcoef(X,Y) converts them to column vectors.
[RHO,PVAL] = corr(X,Y) also returns PVAL, a matrix of p-values for testing the hypothesis of no correlation against the alternative that there is a nonzero correlation. Each element of PVAL is the p value for the corresponding element of RHO. If PVAL(i,j) is small, say less than 0.05, then the correlation RHO(i,j) is significantly different from zero.
[RHO,PVAL] = corr(X,Y,'name',value) specifies one or more optional name/value pairs. Specify name inside single quotes. The following table lists valid parameters and their values.
— The alternative hypothesis against which to compute p-values for testing the hypothesis of no correlation
Using the 'pairwise' option for the rows parameter may return a matrix that is not positive definite. The 'complete' option always returns a positive definite matrix, but in general the estimates are based on fewer observations.
corr computes p-values for Pearson's correlation using a Student's t distribution for a transformation of the correlation. This correlation is exact when X and Y are normal. corr computes p-values for Kendall's tau and Spearman's rho using either the exact permutation distributions (for small sample sizes), or large-sample approximations.
corr computes p-values for the two-tailed test by doubling the more significant of the two one-tailed p-values.
Find the correlation between two matrices and compare to the correlation between two column vectors.
Generate sample data.
rng('default') x = randn(30,4); y = randn(30,4); y(:,4) = sum(x,2); % introduce correlation
Calculate the correlation between columns of X and Y.
[r,p] = corr(x,y)
r = -0.1686 -0.0363 0.2278 0.6901 0.3022 0.0332 -0.0866 0.2617 -0.3632 -0.0987 -0.0200 0.3504 -0.1365 -0.1804 0.0853 0.4908 p = 0.3731 0.8489 0.2260 0.0000 0.1045 0.8619 0.6491 0.1624 0.0485 0.6039 0.9166 0.0577 0.4721 0.3400 0.6539 0.0059
Calculate the correlation between X and Y using corrcoef.
[r,p] = corrcoef(x,y)
r = 1.0000 0.1252 0.1252 1.0000 p = 1.0000 0.1729 0.1729 1.0000
MATLAB function corrcoef converts X and Y into column vectors before computing the correlation between them.
 Gibbons, J.D. (1985) Nonparametric Statistical Inference, 2nd ed., M. Dekker.
 Hollander, M. and D.A. Wolfe (1973) Nonparametric Statistical Methods, Wiley.
 Kendall, M.G. (1970) Rank Correlation Methods, Griffin.
 Best, D.J. and D.E. Roberts (1975) "Algorithm AS 89: The Upper Tail Probabilities of Spearman's rho", Applied Statistics, 24:377-379.