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.


Covariance of data samples

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::covariance([x1, x2, …], [y1, y2, …], <Sample | Population>)
stats::covariance([[x1, y1], [x2, y2], …], <Sample | Population>)
stats::covariance(s, <c1, c2>, <Sample | Population>)
stats::covariance(s, <[c1, c2]>, <Sample | Population>)
stats::covariance(s1, <c1>, s2, <c2>, <Sample | Population>)


stats::covariance([x1, x2, …, xn], [y1, y2, …, yn]) returns the covariance


where and are the arithmetic means of the data xi and yi, respectively.

stats::covariance([x1, x2, …, xn], [y1, y2, …, yn], Population) returns


If the input data are floating-point numbers, the sums defining the covariance are computed in a numerically stable way. If a floating point result is desired, it is recommended to make sure that all input data are floats.

For exact input data, exact symbolic expressions are returned.

The column indices c1, c2 are optional if the data are given by a stats::sample object s containing only two non-string data columns. If the data are provided by two samples s1, s2, the column indices are optional for samples containing only one non-string data column.

External statistical data stored in an ASCII file can be imported into a MuPAD® session via import::readdata. In particular, see Example 1 of the corresponding help page.


Example 1

We compute the covariance of samples passed as lists:

X := [2, 33/7, 21/9, PI]: Y :=  [3, 5, 1, 7]:
stats::covariance(X, Y)

Alternatively, the data may be passed as a list of data pairs:

stats::covariance([[2, 3], [33/7, 5], [21/9, 1], [PI, 7]])

If all data are floating-point numbers, the result is a float:

stats::covariance(float(X), float(Y))

delete X, Y:

Example 2

We create a sample of type stats::sample:

s := stats::sample([[1.0, 2.4, 3.0],
                    [7.0, 4.8, 4.0],
                    [3.3, 3.0, 5.0]])
1.0  2.4  3.0
7.0  4.8  4.0
3.3  3.0  5.0

We compute the covariance of the first column and the third column in several equivalent ways:

stats::covariance(s, 1, 3),
stats::covariance(s, [1, 3]),
stats::covariance(s, 1, s, 3)

delete s:

Example 3

The covariance of symbolic data is returned as a symbolic expression:

stats::covariance([x1, x2], [y1, y2])



x1, y1, x2, y2, …

The statistical data: arithmetical expressions. The number of data xi must coincide with the number of data yi.

s, s1, s2

Samples of type stats::sample

c1, c2

Column indices: positive integers. Column c1 of s or s1, respectively, provides the data xi. Column c2 of s or s2, respectively, provides the data yi.



The data are regarded as a “sample”, not as a full population. This is the default.


The data are regarded as the whole population, not as a sample.

Was this topic helpful?