我正在尝试计算一组给定观察值的均值和协方差矩阵。点列表是一个 3-d 数组,第一维表示类别编号,第二维表示观察编号,第三维表示坐标编号。虽然我已经能够计算出平均值,但协方差似乎存在一些问题(现在,我得到一个零矩阵)。如果有人能告诉我如何纠正它,我将不胜感激。
function [ meanEst, covEst, priorProbEst, classMem ] = estimateParams( trainingSet, classList )
%estimateParams estimate all parameters for each class
numRows = size(trainingSet, 1);
numClasses = max(classList.');
%pointList = zeros(numClasses, numRows, 2);
classMem = zeros(numClasses, 1);
for rowCtr = 1:numRows
curClass = classList(rowCtr, 1);
classMem(curClass) = classMem(curClass) + 1;
pointList(curClass, classMem(curClass), 1) = trainingSet(rowCtr, 1);
pointList(curClass, classMem(curClass), 2) = trainingSet(rowCtr, 2);
end
meanEst = zeros(numClasses, 2);
covEst = zeros(numClasses, 2, 2);
priorProbEst = zeros(numClasses, 1);
tot = zeros(numClasses, 2);
for classCtr = 1:numClasses
for pointCtr = 1:classMem(classCtr)
tot(classCtr, 1) = tot(classCtr, 1) + pointList(classCtr, pointCtr, 1);
tot(classCtr, 2) = tot(classCtr, 2) + pointList(classCtr, pointCtr, 2);
end
meanEst(classCtr, 1) = tot(classCtr, 1) / classMem(classCtr);
meanEst(classCtr, 2) = tot(classCtr, 2) / classMem(classCtr);
covEst(classCtr) = cov(pointList(classCtr));
priorProbEst(classCtr) = classMem(classCtr) / numRows;
end
end
感谢您花时间在这上面!