3D ポイントで構成される一連のクラスターがあります。2 つのクラスターそれぞれから最も近い 2 つのポイントを取得したいと考えています。
例: 3D ポイントで構成される C1 から C5 までの 5 つのクラスターがあります。C1 と C2 には、2 つのクラスター C1 と C2 の間の最も近い 2 つのポイントである 2 つのポイント Pc1 "point in C1" と Pc2 "point in C2" があり、C1 と C3..C5 の間でも同じであり、C2 と C3 の間でも同じです。 .C5 など。その後、異なるクラスター間の最も近い点を表す 20 点が得られます。
2 つ目は、それぞれのポイントと他のポイントの間の距離が特定の距離「しきい値」よりも小さい場合に、このポイントを接続したいということです。
そこで、誰かアドバイスをお願いします
Update:
Amro の回答に感謝します。CIDX=kmeans(X, K,'distance','cityblock', 'replicates',5); に更新しました。空のクラスター エラーを解決します。しかし、別のエラーが表示されました「pdistmex Out of memory. Type HELP MEMORY for your options.」だから私はあなたの答えをここでチェックしました: Out of memory error while using clusterdata in MATLAB and updated your code as below mn = min(min(D(idx1,idx2)));
.エラー?
使用したコード:
%function single_linkage(depth,clrr)
X = randn(5000,3);
%X=XX;
% clr = clrr;
K=7;
clr = jet(K);
%// cluster into K=4
K = 7;
%CIDX = kmeans(X,K);
%// pairwise distances
SUBSET_SIZE = 1000; %# subset size
ind = randperm(size(X,1));
data = X(ind(1:SUBSET_SIZE), :);
D = squareform(pdist(data));
subs = 1:size(D,1);
CIDX=kmeans(D, K,'distance','sqEuclidean', 'replicates',5);
centers = zeros(K, size(data,2));
for i=1:size(data,2)
centers(:,i) = accumarray(CIDX, data(:,i), [], @mean);
end
%# calculate distance of each instance to all cluster centers
D = zeros(size(X,1), K);
for k=1:K
D(:,k) = sum( bsxfun(@minus, X, centers(k,:)).^2, 2);
end
%D=squareform(D);
%# assign each instance to the closest cluster
[~,clustIDX] = min(D, [], 2);
%// for each pair of clusters
cpairs = nchoosek(1:K,2);
pairs = zeros(size(cpairs));
dists = zeros(size(cpairs,1),1);
for i=1:size(cpairs,1)
%// index of points assigned to each of the two cluster
idx1 = (clustIDX == cpairs(i,1));
idx2 = (clustIDX == cpairs(i,2));
%// shortest distance between the two clusters
mn = min(min(D(idx1,idx2)));
dists(i) = mn;
%// corresponding pair of points with the minimum distance
[r,c] = find(D(idx1,idx2)==mn);
s1 = subs(idx1); s2 = subs(idx2);
pairs(i,:) = [s1(r) s2(c)];
end
%// filter pairs by keeping only those whose distances is below a threshold
thresh = inf;
cpairs(dist>thresh,:) = [];
%// plot 3D points color-coded by clusters
figure('renderer','zbuffer')
%clr = lines(K);
h = zeros(1,K);
for i=1:K
h(i) = line(X(CIDX==i,1), X(CIDX==i,2), X(CIDX==i,3), ...
'Color',clr(i,:), 'LineStyle','none', 'Marker','.', 'MarkerSize',5);
end
legend(h, num2str((1:K)', 'C%d')) %'
view(3), axis vis3d, grid on
%// mark and connect nearest points between each pair of clusters
for i=1:size(pairs,1)
line(X(pairs(i,:),1), X(pairs(i,:),2), X(pairs(i,:),3), ...
'Color','k', 'LineStyle','-', 'LineWidth',3, ...
'Marker','o', 'MarkerSize',10);
end