# corrcov

Convert covariance matrix to correlation matrix

## Description

## Examples

### Compare Correlation Matrices Obtained by Two Different Methods

Compare the correlation matrix obtained by applying `corrcov`

on a covariance matrix with the correlation matrix obtained by direct computation using `corrcoef`

on an input matrix.

Load the `hospital`

data set and create a matrix containing the `Weight`

and `BloodPressure`

measurements. Note that `hospital.BloodPressure`

has two columns of data.

```
load hospital
X = [hospital.Weight hospital.BloodPressure];
```

Compute the covariance matrix.

C = cov(X)

`C = `*3×3*
706.0404 27.7879 41.0202
27.7879 45.0622 23.8194
41.0202 23.8194 48.0590

Compute the correlation matrix from the covariance matrix by using `corrcov`

.

R1 = corrcov(C)

`R1 = `*3×3*
1.0000 0.1558 0.2227
0.1558 1.0000 0.5118
0.2227 0.5118 1.0000

Compute the correlation matrix directly by using `corrcoef`

, and then compare `R1`

with `R2`

.

R2 = corrcoef(X)

`R2 = `*3×3*
1.0000 0.1558 0.2227
0.1558 1.0000 0.5118
0.2227 0.5118 1.0000

The correlation matrices `R1`

and `R2`

are the same.

### Find Standard Deviations from Covariance Matrix

Find the vector of standard deviations from the covariance matrix, and show the relationship between the standard deviations and the covariance matrix.

Load the `hospital`

data set and create a matrix containing the `Weight`

, `BloodPressure`

, and `Age`

measurements. Note that `hospital.BloodPressure`

has two columns of data.

```
load hospital
X = [hospital.Weight hospital.BloodPressure hospital.Age];
```

Compute the covariance matrix of `X`

.

C = cov(X)

`C = `*4×4*
706.0404 27.7879 41.0202 17.5152
27.7879 45.0622 23.8194 6.4966
41.0202 23.8194 48.0590 4.0315
17.5152 6.4966 4.0315 52.0622

`C`

is square, symmetric, and positive semidefinite. The diagonal elements of `C`

are the variances of the four variables in `X`

.

Compute the correlation matrix and standard deviations of `X`

from the covariance matrix `C`

.

[R,s1] = corrcov(C)

`R = `*4×4*
1.0000 0.1558 0.2227 0.0914
0.1558 1.0000 0.5118 0.1341
0.2227 0.5118 1.0000 0.0806
0.0914 0.1341 0.0806 1.0000

`s1 = `*4×1*
26.5714
6.7128
6.9325
7.2154

Compute the square root of the diagonal elements in `C`

, and then compare `s1`

with `s2`

.

s2 = sqrt(diag(C))

`s2 = `*4×1*
26.5714
6.7128
6.9325
7.2154

`s1`

and `s2`

are equal and correspond to the standard deviation of the variables in `X`

.

## Input Arguments

`C`

— Covariance matrix

matrix

Covariance matrix, specified as a square, symmetric, and positive semidefinite matrix.

For a matrix *X* that has *N*
observations (rows) and *n* random variables (columns),
`C`

is an *n*-by-*n*
matrix. The *n* diagonal elements of `C`

are the variances of the *n* random variables in
*X*, and a zero diagonal element in
`C`

indicates a constant variable in
*X*.

**Data Types: **`single`

| `double`

## Output Arguments

`R`

— Correlation matrix

matrix

Correlation matrix, returned as a matrix that corresponds to the
covariance matrix `C`

.

**Data Types: **`single`

| `double`

`sigma`

— Standard deviations

vector

Standard deviations, returned as an
*n*-by-*1* vector.

The elements of `sigma`

are the standard deviations of
the variables in *X*, the
*N*-by-*n* matrix that produces
`C`

. Row `i`

in
`sigma`

corresponds to the standard deviation of column
`i`

in *X*.

**Data Types: **`single`

| `double`

## More About

### Covariance

For two random variable vectors *A* and
*B*, the covariance is defined as

$$\mathrm{cov}(A,B)=\frac{1}{N-1}{\displaystyle \sum _{i=1}^{N}{({A}_{i}-{\mu}_{A})}^{*}({B}_{i}-{\mu}_{B})}$$

where *N* is the length of each column,
*μ _{A}* and

*μ*are the mean values of

_{B}*A*and

*B*, respectively, and

`*`

denotes the complex conjugate.The *covariance matrix* of two random variables is the matrix
of pairwise covariance calculations between each variable,

$$C=\left(\begin{array}{cc}\mathrm{cov}(A,A)& \mathrm{cov}(A,B)\\ \mathrm{cov}(B,A)& \mathrm{cov}(B,B)\end{array}\right).$$

For a matrix *X*, in which each column is a
random variable composed of observations, the covariance matrix is the pairwise
covariance calculation between each column combination. In other words, $$C(i,j)=\mathrm{cov}\left(X(:,i),X(:,j)\right)$$.

### Variance

For a random variable vector *A* composed of
*N* scalar observations, the variance is defined as

$$V=\frac{1}{N-1}{\displaystyle \sum _{i=1}^{N}|{A}_{i}-\mu {|}^{2}}$$

where *μ* is the mean of *A*,

$$\mu =\frac{1}{N}{\displaystyle \sum _{i=1}^{N}{A}_{i}}.$$

Some definitions of variance use a normalization factor of
*N* instead of *N–1*, but the mean always has
the normalization factor *N*.

## Extended Capabilities

### Thread-Based Environment

Run code in the background using MATLAB® `backgroundPool`

or accelerate code with Parallel Computing Toolbox™ `ThreadPool`

.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced in R2007b**

## MATLAB 명령

다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.

명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)