# Fisher Discriminant analysis - issues with classes

2 views (last 30 days)
Commented: KSSV on 1 Feb 2023
Writing a program to do Fisher's discriminant analysis. The data file I'm using is 3 x 500, so n = 3.
The dataset is given in form of a large matrix X = [X1X2 . . . XC] ∈ R n×Ck, where Xc ∈ R n×k is the data for each of the classes c = 1, 2, . . . , C. C = 5 and k = 100
1. Compute mean µc of each class, for c = 1, 2, . . . , C.
2. Compute within class scatter matrix Sw and between class scatter matrix Sb
3. Perform generalized eigen-value decomposition using [V,D] = eig(Sb,Sw,‘chol’);
4. Reorder solution using V = fliplr(V) and D = flipud(fliplr(D)).
5. Set U = V(:,1:d) and project Z = U T X. Plot of the eigenvalues and show a colored 2D scatter plot of the projected data.
What I have so far
% Define parameters
C = 5; % number of classes
k = 100; % number of features
[n,~] = size(X); % number of samples
d = 2; % dimension for projection
% Compute mean of each class
mu = zeros(k,C);
for c = 1:C
Xc = X(:,(c-1)*k+1:c*k);
mu(:,c) = mean(Xc,2);
end
% Compute within class scatter matrix Sw and between class scatter matrix Sb
Sw = zeros(k,k);
Sb = zeros(k,k);
for c = 1:C
Xc = X(:,(c-1)*k+1:c*k);
Nc = size(Xc,2);
dmu = bsxfun(@minus, Xc, mu(:,c));
Sw = Sw + dmu * dmu';
Sb = Sb + Nc * (mu(:,c) - mean(mu,2)) * (mu(:,c) - mean(mu,2))';
end
% Perform generalized eigen-value decomposition
[V,D] = eig(Sb,Sw,'chol');
% Reorder solution
V = fliplr(V);
D = flipud(fliplr(D));
% Set U and project Z
U = V(:,1:d);
Z = U' * X;
% Plot the eigenvalues
figure;
plot(diag(D),'o');
title('Eigenvalues');
% Show a colored 2D scatter plot of the projected data
figure;
colors = ['r','g','b','c','m'];
for c = 1:C
Zc = Z(:,(c-1)*n+1:c*n);
scatter(Zc(1,:),Zc(2,:),[],colors(c));
hold on;
end
title('2D scatter plot of the projected data');
end
i am getting this error message and I'm unsure how to fix it
Unable to perform assignment because the size of the left side is 100-by-1 and the size of the right side is 3-by-1.
Error in (line 12)
mu(:,c) = mean(Xc,2);

KSSV on 1 Feb 2023
You should use:
% Compute mean of each class
mu = zeros(k,C); % k = 3 i.e. number rows and C is number of classes i.e.5
for i = 1:k % loop for each row
for j = 1:C % loop for each class
idx = X(i,:)==j ; % get classes j in row i
mu(i,j) = mean(Xc(i,idx));
end
end
##### 2 CommentsShowHide 1 older comment
KSSV on 1 Feb 2023
This is a different error. Your need to think on your code. You see, how I am selecting the classes. You are using indexing to select the classes. Why?

### Categories

Find more on Discriminant Analysis in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!