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

发布于 2024-12-15 05:18:10 字数 670 浏览 2 评论 0原文

我有2个不同的文件,其中一个是输入矩阵(X),有3823*63个元素(3823个输入和63个特征),另一个是类向量(R),有3823*1个元素;这些元素的值从 0 到 9(有 10 个类别)。

我必须计算每个类的协方差矩阵。到目前为止,我只能计算具有如此多嵌套循环的每个类的平均向量。然而,它让我脑死亡。

还有其他简单的方法吗?


有适合我目的的代码(感谢 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

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; those elements have values from 0 to 9 (there are 10 classes).

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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-12-22 05:18:10

这能满足您的需要吗?

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

此代码循环遍历每个类,选择 R 中与该类的观测值相对应的行,然后从 X 中获取相同的行并计算它们的均值和协方差。它将它们存储在元胞数组中,因此您可以像这样访问结果:

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

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

希望有帮助!

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. 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!

云仙小弟 2024-12-22 05:18:10

不要使用 meansum 作为变量名称,因为它们是有用的 Matlab 内置函数的名称。 (输入docmeandocsum以获得使用帮助)

此外cov将为您计算协方差矩阵。

您可以使用逻辑索引来提取示例。

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

单线

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

Don't use mean and sum as a variable names because they are names of useful Matlab built-in functions. (Type doc mean or doc sum for usage help)

Also cov will calculate the covariance matrix for you.

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);
风筝有风,海豚有海 2024-12-22 05:18:10

首先构建每个类的数据矩阵。
其次计算每个数据矩阵的协方差。

下面的代码执行此操作。

% 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文