# corrcov

Convert covariance matrix to correlation matrix

## Syntax

``R = corrcov(C)``
``[R,sigma] = corrcov(C)``

## Description

example

````R = corrcov(C)` returns the correlation matrix `R` corresponding to the covariance matrix `C`. ```

example

````[R,sigma] = corrcov(C)` also returns `sigma`, a vector of standard deviations.```

## Examples

collapse all

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 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

collapse all

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

collapse all

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

Data Types: `single` | `double`

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`

collapse all

### Covariance

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

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

where N is the length of each column, μA and μB are the mean values of 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}\left(A,A\right)& \mathrm{cov}\left(A,B\right)\\ \mathrm{cov}\left(B,A\right)& \mathrm{cov}\left(B,B\right)\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\left(i,j\right)=\mathrm{cov}\left(X\left(:,i\right),X\left(:,j\right)\right)$.

### Variance

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

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

where μ is the mean of A,

`$\mu =\frac{1}{N}\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.

## Version History

Introduced in R2007b