-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLDAFeatureComp.m
More file actions
50 lines (43 loc) · 1.13 KB
/
LDAFeatureComp.m
File metadata and controls
50 lines (43 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
%% Feature Compression using Linear Discrimination Analysis
%
function [minFeatures] = LDAFeatureComp(features,label)
minFeatures = [];
[num,dim] = size(features);
seg = [];
[label,I] = sort(label);
features = features(I,:);
last = min(label);
seg(end+1) = last;
for i= 1: num
if( label(i) > last)
seg(end+1) = i;
last = label(i);
end
end
seg(end+1) = num+1;
ntype = length(seg) - 1;
m = zeros(ntype, dim);
for i = 1: ntype
m(i,:) = mean(features(seg(i):seg(i+1)-1,:),1);
end
mall = mean(features,1);
Sw = zeros(dim,dim);
for i = 1: ntype
Ni = seg(i+1) - seg(i);
Swi = cov(features(seg(i):seg(i+1) - 1,:), 1) * Ni;
Sw = Sw + Swi;
end
Sb = zeros(dim,dim);
for i = 1: ntype
Ni = seg(i+1) - seg(i);
Sb = Sb + (m(i) - mall) * (m(i) -mall)' * Ni;
end
if det(Sw) ~= 0
iSwSb = Sw\Sb;
[V,D] = eig(iSwSb);
lambda = diag(D);
I = real(lambda) == lambda;
W = V(:,I);
minFeatures = features*W;
end
end