我有2個不同的文件,其中之一是一個輸入矩陣(X),它具有3823 * 63個元素(3823個輸入和63個特徵),另一個是類矢量( R),其具有3823 * 1個元素;那些元素的值從0到9(有10個類)。Matlab不同類的協方差矩陣計算
我必須計算每個類的協方差矩陣。到目前爲止,我只能爲每個具有這麼多嵌套循環的類計算平均向量。然而,它導致我腦死亡。
有沒有其他簡單的方法?
有我的目的代碼(感謝薩姆·羅伯茨):
xTra = importdata('optdigits.tra');
xTra = xTra(:,2:64); % first column's inputs are all zero
rTra = importdata('optdigits.tra');
rTra = rTra(:,65); % classes of the data
c = numel(unique(rTra));
for i = 1:c
rTrai = (rTra==i-1); % Get indices of the elements from the ith class
meanvect{i} = mean(xTra(rTrai,:)); % Calculate their mean
covmat{i} = cov(xTra(rTrai,:)); % Calculate their covariance
end
你確定這是必要的嗎?你可能會有一個完全不同的應用程序,所以如果是這樣的話,我會很單調,但我不熟悉使用類標籤計算協方差。例如,我計算一個cov矩陣作爲PCA的謂詞步驟,但所需的結果只是一個二維矩陣(n x n),給出了特徵的成對協方差(這是我需要的特徵向量計算)。我/ o/w類標籤不是必需的。 – doug
@doug我要編輯問題並添加一個標籤。 – onatm