# 2010/bmi10/EMG: ulda.m

File ulda.m, 1.6 KB (added by adutta1, 9 years ago)

Linear Discriminant Analysis

Line
1function G  = ulda(Data, Class)
2
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4% Input:                                                             %
5%     Data:  data matrix (Each row is a data point)                  %
6%     Class: class label (class 1, ..., k)                           %
7% Output:                                                            %
8%      G:    transformation matrix                                   %
9%%% Anirban Dutta
10%%% a-dutta@northwestern.edu
11%%% Creation date: 7/03/2010
12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13
14k        = max(Class); % number of classes
15
16%-------------------------------------------------------------------------
17[m,n] = size(Data);
18cc    = sum(Data)/m;
19
20for i = 1:k
21        loc                  = find(Class==i);
22        [num,o]              = size(loc);
23
24        if num==1
25                B(loc,1:n)   = 0;
26                B(m+i,1:n)   = 0;
27        else
28                TMP          = sum(Data(loc,1:n))/num;
29
30                B(i,1:n)     = sqrt(num)*(TMP - cc);
31                B(loc+k,1:n) = Data(loc,1:n) - ones(num,1)*TMP;
32        end;
33end;
34
35Hw = B(k+1:k+m, :)';
36
37Hb = B(1:k, :)';
38
39Size_low = rank(Hb);
40
41Ht = (Data - ones(m,1)*cc)';
42
43[U1,D1,V1] = svd(Ht,0);     % Ht = U1 D1 V1'
44s = rank(Ht);
45
46D1 = D1(1:s, 1:s);
47U1 = U1(:,1:s);
48
49d1 = diag(D1);
50d1 = 1./d1;
51D1 = diag(d1);
52
53B = D1*U1'*Hb;
54
55[P,D2, Q] = svd(B);
56
57X = U1*D1*P;
58
59G = X(:, 1:Size_low);
60