File Exchange

image thumbnail

LDA: Linear Discriminant Analysis

version (1.95 KB) by Will Dwinnell
Performs linear discriminant analysis.


Updated 11 Dec 2010

View License

Features of this implementation of LDA:
- Allows for >2 classes
- Permits user-specified prior probabilities
- Requires only base MATLAB (no toolboxes needed)
- Assumes that the data is complete (no missing values)
- Has been verified against statistical software
- "help LDA" provides usage and an example, including conditional probability calculation

Note: This routine always includes the prior probability adjustment to the linear score functions. (Some other LDA software drops this when the user specifies equal prior probabilities.)

Cite As

Will Dwinnell (2019). LDA: Linear Discriminant Analysis (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (26)

Alvin Kho

Hello, How can I use this code to obtain the reduced data for svm classifier ?


vinyas Raju


i mean feature selection


Thanks for sharing the resource. It is working perfectly fine. I have first created a main .m file to call the LDA function. Also I tried executing file when target is Alphanumeric value, it shows an error.
Thank you

Thanks for your resource and your sharing! Perfect.

blue wind

Doesn't run, it says "not enough input arguments".


lu phye

still do not understand the matrix W

zhou zexun

Thanks for your resource and your sharing!

I did not understand the matrix w!
Can you explain it?


thanks for your program


Thank you for your program.

But I agree with maryam, after you find the coefficients for the training data, it should be used to test and classify another data which is missed the class label.
It is great if it is contains a testing part.

@ Christian Johner: where is the file?


it might be worth to mention will's blogspot entry where he explains the code in some more detail and also answers some interesting questions:

thanks for the code, will!

I'm an utter beginner with LDA, but I'm getting quite different class probability results using this vs. the 'classify' routine from the statistics toolbox. Perhaps it's the prior probability adjustment, but it would be nice if this had a literature reference and/or comparable results to classify.

@maryam faal: how it is done is shown in the file.


very good

Hello Mr Dwinnell,

I’m oberstein, PHD student of university of Paris.

Thank you very much for your share of your LDA (discriminant analysis) code, I find it on the web of Matlab center, it is very useful for me, yours is more intelligent than mine o(∩_∩)o

But there are some things of your code that I don’t understand, Can I ask you three questions about your LDA code?

Thank you at first!

1 For Accumulate pooled covariance information, why do you use ((nGroup(i) - 1) / (n - k) ) in “PooledCov = PooledCov + ((nGroup(i) - 1) / (n - k) ).* cov(Input(Group,:))”? Why it isn’t nGroup(i)/ n or nGroup(i)/ n-1 witch we use often in the probability? Can you tell me the raison or the theory with ((nGroup(i) - 1) / (n - k) )?

2 I don’t quite understand you Matrix W.

2-1) In the LDA, we find at first Sw (with-in-class scatter matrix) and Sb (between-class scatter matrix), and then we can find the eigenvectors of inv(Sw)*Sb, isn’t it? What is your matrix W? Is it the eigenvectors? – I don’t think so. Is it the matrix inv(Sw)*Sb? – But why you add the term log(PriorProb(i))?

2-2) Can you tell me something about the term log(PriorProb(i)) ? I don’t understand why it is here in W(:,1). Is it for the linear regression?

3 For calculate class probabilities at last, why do you use exponent? P=exp(L)./repmat(sum(exp(L),2),[1 2]), it can’t be L./ repmat(sum(L),2),[1 2]) ? I don’t understand why we must use the exponent to calculate the probabilities.

Thank you very much
Best Regards


maryam faal

you said that input is training sample then how did you classify the test sample?

Input and Target are both from the training data. "Input" is a matrix containing the independent variables, while "Target" contains the dependent variable.

maryam faal

Dear Will
Thanks for your program but I have a question about it
"Input" and "Target" are training samples or test samples?

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