基于kmeans documentation page 上的示例,我提出了这种“嵌套”逻辑:
X = [randn(100,2)+ones(100,2);...
randn(100,2)-ones(100,2)];
opts = statset('Display','final');
% This gives a random distribution of 0s and 1s in column 5:
X(:,5) = round(rand(size(X,1),1));
[idx,ctrs] = kmeans(X,2,...
'Distance','city',...
'Replicates',5,...
'Options',opts);
hold on
plot(X(idx==1,1),X(idx==1,2),'rs','MarkerSize',12)
plot(X(idx==2,1),X(idx==2,2),'r+','MarkerSize',12)
% after plotting the results of kmeans,
% plot new symbols with a different logic on top:
plot(X(X(idx==1,5)==0,1),X(X(idx==1,5)==0,2),'bs','MarkerSize',12)
plot(X(X(idx==1,5)==1,1),X(X(idx==1,5)==1,2),'gs','MarkerSize',12)
plot(X(X(idx==2,5)==0,1),X(X(idx==2,5)==0,2),'b+','MarkerSize',12)
plot(X(X(idx==2,5)==1,1),X(X(idx==2,5)==1,2),'g+','MarkerSize',12)
上面的代码是一个最小的工作示例,假设统计工具箱可用。
关键特性是绘图的嵌套逻辑。例如:
X(X(idx==1,5)==0,1)
内部X(idx==1,5) 选择X(:,5) 的那些值,idx==1 的值。其中,仅考虑 0 的值:X(X(...)==0,1)。根据问题中的逻辑,这应该是一个蓝色方块:bs。
您有四个案例,因此有四个额外的情节线。