1

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 
2
  • are you sure this is necessary? you might have a completely different application in mind, so parton me if so, but i am not familiar w/ calculating covariance using class labels. E.g., i calculate a cov matrix as the predicate step to PCA, but the required result is just a 2D matrix (n x n) giving pair-wise covariance of the features (which is all i need for eigenvector computation). i/o/w the class labels are not required for this. Commented Nov 13, 2011 at 2:10
  • @doug i am going to edit the question and add one more tag. Commented Nov 13, 2011 at 2:15

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

3 Comments

holy mother of computer god, what kind of sorcery is this? Thank you so much!
Yep - typically the point of MATLAB is that it saves you from having to do all those nested for loops. Matrix calculations are built-in!
i used mean and cov functions before but i didn't used them in cell structure. thank you again for the answer and teaching me how to use cells.
1

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

Comments

0

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 

1 Comment

Yup, the nice matrix indexing is shown above by Sam Roberts solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.