简体   繁体   English

不同类别的Matlab协方差矩阵计算

[英]Matlab Covariance Matrix Computation for Different Classes

I've got 2 different files, one of them is an input matrix (X) which has 3823*63 elements (3823 input and 63 features), the other one is a class vector (R) which has 3823*1 elements; 我有2个不同的文件,其中一个是输入矩阵(X),它具有3823 * 63个元素(3823个输入和63个特征),另一个是类向量(R),它具有3823 * 1个元素; those elements have values from 0 to 9 (there are 10 classes). 这些元素的值从0到9(有10个类)。

I have to compute covariance matrices for every classes. 我必须为每个类计算协方差矩阵。 So far, i could only compute mean vectors for every classes with so many nested loops. 到目前为止,我只能为具有这么多嵌套循环的每个类计算均值向量。 However, it leads me to brain dead. 然而,它导致我脑死亡。

Is there any other easy way? 有没有其他简单的方法?


There is the code for my purpose (thanks to Sam Roberts): 我的目的有代码(感谢Sam Roberts):

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

Does this do what you need? 这样做你需要的吗?

X = rand(3263,63);
R = randi(10,3263,1)-1;

numClasses = numel(unique(R));

for i = 1:numClasses
    Ri = (R==i); % Get indices of the elements from the ith class
    meanvect{i} = mean(X(Ri,:)); % Calculate their mean
    covmat{i} = cov(X(Ri,:)); % Calculate their covariance
end

This code loops through each of the classes, selects the rows of R that correspond to observations from that class, and then gets the same rows from X and calculates their mean and covariance. 此代码循环遍历每个类,选择与该类中的观察对应的R行,然后从X获取相同的行并计算它们的均值和协方差。 It stores them in a cell array, so you can access the results like this: 它将它们存储在单元格数组中,因此您可以访问如下结果:

% Display the mean vector of class 1
meanvect{1}

% Display the covariance matrix of class 2
covmat{2}

Hope that helps! 希望有所帮助!

Don't use mean and sum as a variable names because they are names of useful Matlab built-in functions. 不要将meansum用作变量名,因为它们是有用的Matlab内置函数的名称。 (Type doc mean or doc sum for usage help) (输入doc meandoc sum以获得使用帮助)

Also cov will calculate the covariance matrix for you. 此外, cov将为您计算协方差矩阵。

You can use logical indexing to pull out the examples. 您可以使用逻辑索引提取示例。

covarianceMatrices = cell(m,1);
for k=0:m-1
    covarianceMatrices{k} = cov(xTra(rTra==k,:));
end

One-liner 一衬垫

covarianceMatrices = arrayfun(@(k) cov(xTra(rTra==k,:)), 0:m-1, 'UniformOutput', false);

First construct the data matrix for each class. 首先为每个类构建数据矩阵。 Second compute the covariance for each data matrix. 其次,计算每个数据矩阵的协方差。

The code below does this. 下面的代码执行此操作。

% assume allData contains all the data you've read in, each row is one data point
% assume classVector contains the class of each data point
numClasses = 10;
data = cell(10,1);       %use cells to store each of the data matrices
covariance = cell(10,1); %store each of the class covariance matrices
[numData dummy] = size(allData);

%get the data out of allData and into each class' data matrix
%there is probably a nice matrix way to do this, but this is hopefully clearer
for i = 1:numData
  currentClass = classVector(i) + 1; %Matlab indexes from 1
  currentData = allData(i,:);
  data{currentClass} = [data{currentClass}; currentData];
end

%calculate the covariance matrix for each class
for i = 1:numClasses
  covariance{i} = cov(data{i});
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM