我有一个1000x6的数据集并且使用下面的kmeans脚本很好但是当我想输出其中一个集群时它只作为一列出现?
%% cluster
opts = statset('MaxIter', 100, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',6);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 5, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 100, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K); % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)
% Output cluster data to K datasets
K1 = data(clustIDX==1)
K2 = data(clustIDX==2)... etc
不应该K1 = data(clustIDX==1)
输出完整的行信息吗?不只是一列而是六像原始数据集一样?或者这只是输出距离?
答案 0 :(得分:2)
替换
K1 = data(clustIDX==1)
K2 = data(clustIDX==2)
与
K1 = data(clustIDX==1,:)
K2 = data(clustIDX==2,:)
第一个只检索相应行的第一列。第二个应该解决它,我已经尝试过并且有效。