File Exchange

## Linear Discriminant Analysis Code

version 1.1.0.0 (1.95 KB) by
this function converts data from its original space to LDA space.

17 Downloads

Updated 16 Nov 2011

View License

% [sLDA WLDA M WPCA]=mylda(data,class,n)
% this function written by muhammet balcilar
% yildiz technical university computer engineering department
% istanbul turkiye 2011

% this function convert data from its original space to LDA space
% if number of data samples is less than number of diamension, PCA is
% implemented for reducing number of diamension to #samples-1.
% after PCA, LDA is implemented for reducing diamention to n.

% data is consist of M rows(sample size), N cols(dimensions)
% class is consist of M rows(sample size), 1 cols , each element of class
% is shows class number of each data sample
% (class number must be integer 1 to classsize)
% n is the number of outputs data diamensions.(optionally)
% sLDA is consist of M rows(sample size) n cols(new dimensions)
% WPCA is translate matrix which convert to original space to PCA space
% M is the mean vector of training set
% WLDA is the translate matrix which convert to original space to LDA space
% exaple: there are 4 samples which have 5 diamensions.first two samples
% are member of class 1 others are member of class 2.
% Train= [5.6,5.7,5.5,5.7 5.6;
% 5.7,5.3,5.1,5.0 5.2;
% 10.6,9.9,10.4,10.7 10.2;
% 10.7,9.8,9.9,10 10];
% Class=[1;1;2;2];
% [sLDA WLDA M WPCA]=mylda(Train,Class)
% Test= [4.9 5.5 4.8 5.7 5];
% LDATEST = (Test-M)*WPCA*WLDA

### Cite As

Muhammet (2021). Linear Discriminant Analysis Code (https://www.mathworks.com/matlabcentral/fileexchange/33768-linear-discriminant-analysis-code), MATLAB Central File Exchange. Retrieved .

### Comments and Ratings (10)

A Aftab

Waleed Maswadeh

to make it work. with many classes minor changes needed:

% LDA start
yO=InputDataInv;
M=zeros(1,InputDataColumns);
WPCA=1;
mU=mean(InputData)';% mean of each variable
mK=[];
n=[];
for i=1:length(ClassLabel) % number of classes
I=find(ClassesID==i);
n=[n length(I)]; %-----------------
ort=(mean(InputDataInv(:,I)'))';
mK=[mK ort]; % mean of each variables at each class
for j=1:length(I)
UU(:,I(j))=InputDataInv(:,I(j))-ort; %each class is mean-centered by its own mean
end
end
% UU = InputDataInv but mean centered by:each class is mean-centered by its own mean

for i=1:length(ClassLabel)
I=find(ClassesID==i);
S{i}= UU(:,I)*UU(:,I)';
end
SW=S{1};
for i=2:length(ClassLabel)
SW=SW+S{i};
end

%mK = mean of each variables at each class
%mU =mean of each variable
for i=1:length(ClassLabel)
mmK(:,i)=mK(:,i)-mU; % each class means are substracted from variable-means of input-dataset
end

SB=0;
%temp=0;
for i=1:length(ClassLabel)
%temp=mmK(:,i)*mmK(:,i)';
SB=SB+ n(i)*mmK(:,i)*mmK(:,i)'; % each class means are substracted from variable-means of input-dataset
end

[w u]=eig(SB,SW);

lambda=abs(diag(u));
lambda_sorted=[lambda [1:length(u)]'];
[lambda, SortOrder]=sort(lambda,'descend');
WLDA=w(:,SortOrder); % number of dimensions in LDA is nClasses-1
sLDA=(InputData*WLDA)';

OutputData_LDA=sLDA';
gscatter(OutputData_LDA(:,1), OutputData_LDA(:,2),ClassesID);

SreeSanjanaa Bose

@Waleed Maswadeh how many classes u had. Did it work can u help me

Waleed Maswadeh

works like a charm...........Thanks

blue wind

Did not run, "not enough input arguments"

arwa raad

how i interpret the output of this step
LDATEST = (Test-M)*WPCA*WLDA

Elahe ordoni

Georgina

Thanks for the code!!

sadaf Behlim

Thanks alot for the code.
I have 10 classes in handwritten digit recognition case. But this program results in a vector which are not the label of the classes. Please Help

Muhammet

this program runs with multi class too.

##### MATLAB Release Compatibility
Created with R2008a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux

### Community Treasure Hunt

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

Start Hunting!