-
Notifications
You must be signed in to change notification settings - Fork 36
/
kShape.m
81 lines (65 loc) · 1.44 KB
/
kShape.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function [mem cent] = kShape(A, K)
m=size(A, 1);
mem = ceil(K*rand(m, 1));
cent = zeros(K, size(A, 2));
for iter = 1:100
disp(iter);
prev_mem = mem;
for k = 1:K
cent(k,:) = kshape_centroid(mem, A, k, cent(k,:));
cent(k,:) = zscore(cent(k,:));
end
for i = 1:m
%x = A(i,:);
for k = 1:K
%y = cent(k,:);
dist = 1-max( NCCc( A(i,:), cent(k,:)) );
D(i,k) = dist;
end
end
[val mem] = min(D,[],2);
if norm(prev_mem-mem) == 0
break;
end
end
end
function ksc = kshape_centroid(mem, A, k, cur_center)
% Slower version
%Computes ksc centroid
%a = [];
%for i=1:length(mem)
% if mem(i) == k
% if sum(cur_center) == 0
% opt_a = A(i,:);
% else
% [tmp tmps opt_a] = SBD(zscore(cur_center), A(i,:));
% end
% a = [a; opt_a];
% end
%end
a = A(mem==k,:);
if sum(cur_center) ~= 0
for i=1:size(a,1)
[tmp tmps opt_a] = SBD(cur_center, a(i,:));
a(i,:) = opt_a;
end
end
if size(a,1) == 0
ksc = zeros(1, size(A,2));
return;
end
[m, ncolumns]=size(a);
[Y mean2 std2] = zscore(a,[],2);
S = Y' * Y;
P = (eye(ncolumns) - 1 / ncolumns * ones(ncolumns));
M = P*S*P;
[V D] = eigs(M,1);
ksc = V(:,1);
finddistance1 = sqrt(sum((a(1,:) - ksc').^2));
finddistance2 = sqrt(sum((a(1,:) - (-ksc')).^2));
if (finddistance1<finddistance2)
ksc = ksc;
else
ksc = -ksc;
end
end