View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
LDA: Linear Discriminant Analysis

4.5 | 11 ratings Rate this file 227 Downloads (last 30 days) File Size: 1.95 KB File ID: #29673 Version: 1.0

LDA: Linear Discriminant Analysis


Will Dwinnell (view profile)


Performs linear discriminant analysis.

| Watch this File

File Information

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

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
25 Jan 2017 Mohd Zuli Jaafar

Thanks for your resource and your sharing! Perfect.

08 Nov 2016 blue wind

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

Comment only
19 Sep 2016 bartolo onotsyo


Comment only
03 Oct 2015 lu phye

still do not understand the matrix W

25 Jun 2015 zhou zexun

Thanks for your resource and your sharing!

26 Aug 2014 Chunsik Choe

07 Feb 2014 tiene filisbino

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

16 Dec 2013 Lu

Lu (view profile)

thanks for your program

Comment only
05 Sep 2013 Ghulam Rasool

07 Mar 2013 Qasem

Qasem (view profile)

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?

Comment only
16 May 2012 Joon

Joon (view profile)

23 Mar 2012 Robert Preusche

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!

Comment only
02 Feb 2012 Greg Fichter

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.

29 Nov 2011 Christian Johner

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

03 Aug 2011 sun

sun (view profile)

very good

30 Jun 2011 oberstein ova

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


13 Apr 2011 maryam faal

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

Comment only
07 Apr 2011 Will Dwinnell

Will Dwinnell (view profile)

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

Comment only
06 Apr 2011 maryam faal

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

Comment only

Contact us