-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnt_cluster1D.m
executable file
·33 lines (26 loc) · 969 Bytes
/
nt_cluster1D.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
function [C,A,score]=nt_cluster1D(x);
%[C,A,score]=nt_cluster1D_b(x) - cluster 1D data into 2 clusters
%
% x: column vector or matrix of data to cluster
%
% C: centroid pairs (one pair per column)
% A: ownership matrix (0, 1)
% score: energy/total energy, for each column
if nargin<1; error('!'); end
if size(x,1)<2; error('too small to cluster'); end
A=zeros(size(x)); % cluster ownership labels
C=zeros(2,size(x,2)); % centroids
for iCol=1:size(x,2)
xx=x(:,iCol);
[xx,iSort]=sort(xx);
[idx,score_vector,score0]=nt_split(xx);
score(:,iCol)=score0;
C(:,iCol)=[mean(xx(1:idx)),mean(xx(idx+1:end))];
t=1:size(xx,1);
A(t(iSort(idx+1:end)), iCol)=1; % 0: low values, 1: high values
% figure(1); clf; subplot 211;
% hold on; histogram(xx,-5:0.01:5, 'displaystyle','stairs' ); histogram(xx(1:idx),-5:0.01:5); plot(C(:,iCol),[500 500], '.r');
% subplot 212; plot(x(:,iCol))
% disp(score0)
% pause
end