forked from viggin/yan-prtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smpSel_cluster.m
45 lines (37 loc) · 1.09 KB
/
smpSel_cluster.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
function [ smpList ] = smpSel_cluster( X,nSel,param )
%Representative sample selection based on cluster centers
%
% X : each row is a sample.
% NSEL : number of samples to select
% PARAM: struct of parameters. The beginning part of this code (before
% defParam) explains each parameter, and also sets the default parameters.
% You can change parameter p to x by setting PARAM.p = x. For parameters
% that are not set, default values will be used.
% Return:
% SMPLIST: indices (of rows in X) of the selected samples.
%
% Ke YAN, 2016, Tsinghua Univ. http://yanke23.com, [email protected]
method = 1; % 1: Agglomerative cluster, 2: kmeans cluster
defParam
[nSmp,nFt] = size(X);
if nSel >= nSmp,
nSel = nSmp;
end
if method == 1
IDX = clusterdata(X,'maxclust',nSel,'linkage','average'); % Agglomerative cluster
elseif method == 2
[IDX,C] = kmeans(X,nSel); % kmeans cluster
end
for p = 1:nSel
idx1 = find(IDX==p);
idx2 = selCenterSmp(X(idx1,:));
smpList(p) = idx1(idx2);
end
end
function idx = selCenterSmp(data)
if size(data,1) == 1, idx = 1;
else
med = median(data,1);
idx = knnsearch(data,med);
end
end