USER
So, I've come with the following implementation since your's making me stuck with the same error. But here I got one more error. Can you fix all the loopholes and errors in the code. Note: The two variables in Face.mat are as follows
Variable Size Data Type Bytes
II 100820x72 double 50872320
m 1x72 double 576. Error is>> project_4
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first
matrix matches the number of rows in the second matrix. To operate on each element of the
matrix individually, use TIMES (.*) for elementwise multiplication.
Error in project_4 (line 46)
projTrainData = trainData' * EigVec; and the code is : % Load the dataset
load('Face.mat');
% Reshape the II matrix into a 72 x 1405 matrix
II = reshape(II, [], size(m, 2));
% Separate the samples for training and testing
trainData = zeros(size(II, 1), 36);
testData = zeros(size(II, 1), 36);
trainLabelsGender = zeros(1, 36);
testLabelsGender = zeros(1, 36);
trainLabelsEmotions = zeros(1, 36);
testLabelsEmotions = zeros(1, 36);
trainLabelsAge = zeros(1, 36);
testLabelsAge = zeros(1, 36);
for i = 1:6
% Use one sample of each emotion for training and the other for testing
trainIndex = (i-1)*6+1;
testIndex = trainIndex + 1;
% Assign the samples to training and testing datasets
trainData(:, trainIndex) = II(:, trainIndex);
trainData(:, trainIndex+1) = II(:, testIndex);
testData(:, testIndex) = II(:, testIndex);
testData(:, testIndex-1) = II(:, trainIndex);
% Assign the gender labels
trainLabelsGender(trainIndex:trainIndex+1) = repmat(m(i), [1 2]);
testLabelsGender(testIndex-1:testIndex) = repmat(m(i), [1 2]);
% Assign the emotion labels
trainLabelsEmotions(trainIndex:trainIndex+1) = [1 2];
testLabelsEmotions(testIndex-1:testIndex) = [1 2];
% Assign the age labels
trainLabelsAge(trainIndex:trainIndex+1) = [i i];
testLabelsAge(testIndex-1:testIndex) = [i i];
end
% Calculate the PCA on the training dataset
[EigVec, EigVal] = eig(cov(trainData));
% Project the training and testing data onto the Eigenfaces
projTrainData = trainData' * EigVec;
projTestData = testData' * EigVec;
% Feature selection using sequential forward search algorithm
topFeaturesGender = sequentialfs(@linearClassifier, projTrainData, trainLabelsGender, 'cv', 'none', 'nfeatures', 6);
topFeaturesEmotions = sequentialfs(@linearClassifier, projTrainData, trainLabelsEmotions, 'cv', 'none', 'nfeatures', 6);
topFeaturesAge = sequentialfs(@linearClassifier, projTrainData, trainLabelsAge, 'cv', 'none', 'nfeatures', 6);
% Train the linear classifier for gender classification
trainDataGender = projTrainData(:, topFeaturesGender);
testDataGender = projTestData(:, topFeaturesGender);
BGender = linearClassifier(trainDataGender, trainLabelsGender);
predictedLabelsGender = BGender(1) + testDataGender' * BGender(2:end)';
predictedLabelsGender(predictedLabelsGender >= 0) = 1;
predictedLabelsGender(predictedLabelsGender < 0) = -1;
% Train the linear classifier for emotion classification
trainDataEmotions = projTrainData(:, topFeaturesEmotions);
testDataEmotions = projTestData(:, topFeaturesEmotions);
BEmotions = linearClassifier(trainDataEmotions, trainLabelsEmotions);
predictedLabelsEmotions = BEmotions(1) + testDataEmotions' * BEmotions(2:end)';
predictedLabelsEmotions = round(predictedLabelsEmotions);
% Train the linear classifier for age classification
trainDataAge = projTrainData(:, topFeaturesAge);
testDataAge = projTestData(:, topFeaturesAge);
BAge = linearClassifier(trainDataAge, trainLabelsAge);
predictedLabelsAge = BAge(1) + testDataAge' * BAge(2:end)';
predictedLabelsAge(predictedLabelsAge < 0) = 1;
predictedLabelsAge(predictedLabelsAge > 2) = 3;
predictedLabelsAge(predictedLabelsAge > 1 & predictedLabelsAge < 3) = 2;
% Evaluate the classification results
confusionMatrixGender = confusionmat(testLabelsGender, predictedLabelsGender);
accuracyGender = sum(diag(confusionMatrixGender))/sum(confusionMatrixGender(:));
confusionMatrixEmotions = confusionmat(testLabelsEmotions, predictedLabelsEmotions);
accuracyEmotions = sum(diag(confusionMatrixEmotions))/sum(confusionMatrixEmotions(:));
confusionMatrixAge = confusionmat(testLabelsAge, predictedLabelsAge);
accuracyAge = sum(diag(confusionMatrixAge))/sum(confusionMatrixAge(:));
fprintf('Accuracy for gender classification: %.2f%%\n', accuracyGender100);
fprintf('Accuracy for emotions classification: %.2f%%\n', accuracyEmotions100);
fprintf('Accuracy for age classification: %.2f%%\n', accuracyAge*100);
ASSISTANT
It seems that the error is caused by the formulation of the `cov()` function. Try using `cov(trainData')` instead of `cov(trainData)` to compute the covariance matrix of the training data. Also, you should adjust the data reshaping process.
Here's the updated code with the fixes for the errors:
```matlab
% Load the dataset
load('Face.mat');
% Reshape the II matrix into a 100820 x 72 matrix
II = reshape(II, 100820, 72);
% Separate the samples for training and testing
trainData = zeros(size(II, 1), 36);
testData = zeros(size(II, 1), 36);
trainLabelsGender = zeros(1, 36);
testLabelsGender = zeros(1, 36);
trainLabelsEmotions = zeros(1, 36);
testLabelsEmotions = zeros(1, 36);
trainLabelsAge = zeros(1, 36);
testLabelsAge = zeros(1, 36);
for i = 1:6
% Use one sample of each emotion for training and the other for testing
trainIndex = (i-1)*6+1;
testIndex = trainIndex + 1;
% Assign the samples to training and testing datasets
trainData(:, trainIndex) = II(:, trainIndex);
trainData(:, trainIndex+1) = II(:, testIndex);
testData(:, testIndex) = II(:, testIndex);
testData(:, testIndex-1) = II(:, trainIndex);
% Assign the gender labels
trainLabelsGender(trainIndex:trainIndex+1) = repmat(m(i), [1 2]);
testLabelsGender(testIndex-1:testIndex) = repmat(m(i), [1 2]);
% Assign the emotion labels
trainLabelsEmotions(trainIndex:trainIndex+1) = [1 2];
testLabelsEmotions(testIndex-1:testIndex) = [1 2];
% Assign the age labels
trainLabelsAge(trainIndex:trainIndex+1) = [i i];
testLabelsAge(testIndex-1:testIndex) = [i i];
end
% Calculate the PCA on the training dataset
[EigVec, EigVal] = eig(cov(trainData'));
% Project the training and testing data onto the Eigenfaces
projTrainData = trainData' * EigVec;
projTestData = testData' * EigVec;
% Feature selection using sequential forward search algorithm
topFeaturesGender = sequentialfs(@linearClassifier, projTrainData, trainLabelsGender, 'cv', 'none', 'nfeatures', 6);
topFeaturesEmotions = sequentialfs(@linearClassifier, projTrainData, trainLabelsEmotions, 'cv', 'none', 'nfeatures', 6);
topFeaturesAge = sequentialfs(@linearClassifier, projTrainData, trainLabelsAge, 'cv', 'none', 'nfeatures', 6);
% Train the linear classifier for gender classification
trainDataGender = projTrainData(:, topFeaturesGender);
testDataGender = projTestData(:, topFeaturesGender);
BGender = linearClassifier(trainDataGender, trainLabelsGender);
predictedLabelsGender = BGender(1) + testDataGender' * BGender(2:end)';
predictedLabelsGender(predictedLabelsGender >= 0) = 1;
predictedLabelsGender(predictedLabelsGender < 0) = -1;
% Train the linear classifier for emotion classification
trainDataEmotions = projTrainData(:, topFeaturesEmotions);
testDataEmotions = projTestData(:, topFeaturesEmotions);
BEmotions = linearClassifier(trainDataEmotions, trainLabelsEmotions);
predictedLabelsEmotions = BEmotions(1) + testDataEmotions' * BEmotions(2:end)';
predictedLabelsEmotions = round(predictedLabelsEmotions);
% Train the linear classifier for age classification
trainDataAge = projTrainData(:, topFeaturesAge);
testDataAge = projTestData(:, topFeaturesAge);
BAge = linearClassifier(trainDataAge, trainLabelsAge);
predictedLabelsAge = BAge(1) + testDataAge' * BAge(2:end)';
predictedLabelsAge(predictedLabelsAge < 0) = 1;
predictedLabelsAge(predictedLabelsAge > 2) = 3;
predictedLabelsAge(predictedLabelsAge > 1 & predictedLabelsAge < 3) = 2;
% Evaluate the classification results
confusionMatrixGender = confusionmat(testLabelsGender, predictedLabelsGender);
accuracyGender = sum(diag(confusionMatrixGender))/sum(confusionMatrixGender(:));
confusionMatrixEmotions = confusionmat(testLabelsEmotions, predictedLabelsEmotions);
accuracyEmotions = sum(diag(confusionMatrixEmotions))/sum(confusionMatrixEmotions(:));
confusionMatrixAge = confusionmat(testLabelsAge, predictedLabelsAge);
accuracyAge = sum(diag(confusionMatrixAge))/sum(confusionMatrixAge(:));
fprintf('Accuracy for gender classification: %.2f%%\n', accuracyGender*100);
fprintf('Accuracy for emotions classification: %.2f%%\n', accuracyEmotions*100);
fprintf('Accuracy for age classification: %.2f%%\n', accuracyAge*100);
```
This code should fix the dimension error you were experiencing. Please note that the dataset and the labels in the `Face.mat` file have not been provided, so the results based on the provided labels may not be accurate. You need to ensure that the labels are aligned correctly with the dataset for the classification to be accurate.