为什么MATLAB本机函数cov(协方差矩阵计算)使用与我预期不同的除数? [英] Why does MATLAB native function cov (covariance matrix computation) use a different divisor than I expect?
问题描述
给出M个维度和N个样本的数据矩阵数据,
Given a data matrix data of M dimensions and N samples, say,
data = randn(N, M);
我可以用以下公式计算协方差矩阵
I could compute the covariance matrix with
data_mu = data - ones(N, 1)*mean(data);
cov_matrix = (data_mu'*data_mu)./N
如果我使用本机MATLAB函数
If I use the native MATLAB function
cov_matrix2 = cov(data)
这将始终等于
cov_matrix = (data_mu'*data_mu)./(N-1)
也就是说,分母(N-1)少一.
That is, the denominator is (N - 1) is one less.
为什么?你能复制吗?这是一个错误吗?
Why?? Can you reproduce it? Is this a bug??
我使用的是MATLAB版本7.6.0.324(2008).
I use MATLAB version 7.6.0.324 (2008).
推荐答案
即,分母为(N-1)少一. 为什么??你能复制吗?这是一个错误吗?
That is, the denominator is (N - 1) is one less. Why?? Can you reproduce it? Is this a bug??
还请注意,如果希望使用分母N而不是N-1,则可以在调用中添加尾随1
参数,即根据文档添加cov(x,y,1)
或cov(x,1)
.
Note also that if you wish to use the denominator N instead of N-1, you can add a trailing 1
argument to the call, i.e. cov(x,y,1)
or cov(x,1)
as per the documentation.
这篇关于为什么MATLAB本机函数cov(协方差矩阵计算)使用与我预期不同的除数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!