This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Compute the correlation matrix associated with a covariance matrix

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.




stats::correlationMatrix(cov) returns to correlation matrix cor of the variance-covariance matrix cov. It is given by:


A covariance matrix C should be positive (semi-)definite and hence satisfies |Cij|2CiiCjj for all indices i, j. Consequently, the absolute values of the entries of the corresponding correlation matrix do not exceed 1.

With the option CovarianceMatrix, the routine stats::reg returns the variance-covariance matrix of the fit parameters in a regression analysis. The corresponding correlation matrix of the fit parameters is computed conveniently by applying stats::correlationMatrix to this matrix. Cf. Example 2.


Example 1

We generate a positive definite matrix that may serve as a covariance matrix:

A := matrix([[4, -3, 2], [-1, 2, 1], [0, 1, 1]]):
cov := A*linalg::transpose(A)

The corresponding correlation matrix is:


If the input matrix consists of floating-point data, the result is a matrix of floats:


delete A, cov:

Example 2

We consider a covariance matrix arising in a non-linear regression problem. The model function y = a + bcos(x - c) is to be fit to the following randomized data:

r := stats::uniformRandom(-0.1, 0.1):
xdata := [i $i = 1..100]:
ydata := [1 + 2*cos(x - 3) + r() $ x in xdata]:

By construction, the variance of the y values is the variance of the uniformly distributed random data on the interval [- 0.1, 0.1] generated by the random generator r. This variance is . We use stats::reg to obtain estimates of the fit parameters a, b, c of the model. Appropriate weights for the regression are given by . The option CovarianceMatrix makes stats::reg include the covariance matrix cov of the fit parameters in its return list:

weights := [300 $ i = 1..100]:
[abc, chisquare, cov] :=
   stats::reg(xdata, ydata, weights, a + b*cos(x - c), 
              [x], [a, b, c], StartingValues = [1, 2, 3],

The correlation matrix of the parameters a, b, c is obtained via stats::correlationMatrix applied to the covariance matrix cov returned by stats::reg:


delete r, xdata, ydata, weights, DIGITS, abc, chisquare, cov:



The covariance matrix: a square matrix of category Cat::Matrix, or an array.

Return Values

Matrix of the same dimension and type as the input matrix cov. FAIL is returned if at least one of the diagonal elements of the input matrix cov is zero.

Was this topic helpful?