-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClustering.m
executable file
·44 lines (35 loc) · 959 Bytes
/
Clustering.m
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
function [mu,Sigma]=Clustering(X,w,minVar)
% initially, all measurements are one cluster
C1.X=X;
C1.w=w;
C1=calc(C1);
nodes=[C1];
while (max([nodes.lambda])>minVar)
nodes=split(nodes);
end
for i=1:length(nodes)
mu(:,i)=nodes(i).q;
Sigma(:,:,i)=nodes(i).R;
end
% calculates cluster statistics
function C=calc(C)
W=sum(C.w);
% weighted mean
C.q=sum(repmat(C.w,[1,size(C.X,2)]).*C.X,1)/W;
% weighted covariance
t=(C.X-repmat(C.q,[size(C.X,1),1])).*(repmat(C.w,[1,size(C.X,2)]));
C.R=(t'*t)/W + 1e-5*eye(3);
[V,D]=eig(C.R);
C.e=V(:,3);
C.lambda=D(9);
% splits maximal eigenvalue node in direction of maximal variance
function nodes=split(nodes)
[~,i]=max([nodes.lambda]);
Ci=nodes(i);
idx=Ci.X*Ci.e<=Ci.q*Ci.e;
Ca.X=Ci.X(idx,:); Ca.w=Ci.w(idx);
Cb.X=Ci.X(~idx,:); Cb.w=Ci.w(~idx);
Ca=calc(Ca);
Cb=calc(Cb);
nodes(i)=[]; % remove the i'th nodes and replace it with its children
nodes=[nodes,Ca,Cb];