目录
1. 自己编写协方差程序和cov()对比
(1)自己编写协方差算法
clc;
clear all;
OriginMatrix = [2,3,2; 4,5,3; 6,4,2; 4,5,3];
[M,N]=size(OriginMatrix); %行,列
MeanArray = mean( OriginMatrix );
MeanMatrix = ones( M, 1 ) * MeanArray;
% 得到每列减去每列均值的的矩阵
OriginSubMean = OriginMatrix - MeanMatrix;
% 计算出相关矩阵
if M == 1
CovMatrixComputed = OriginSubMean' * OriginSubMean / M;
else
CovMatrixComputed = OriginSubMean' * OriginSubMean / ( M-1 );
end
结果:</