问题描述
我有一个矩阵,X,其中我想使用kmeans函数绘制它。我想要的:如果行在第4列中的值为1,我想它是正方形如果该行在第4列中的值为2我想它+形状BUT如果该行的值为0,列5,它必须是蓝色的,如果该行在第5列中的值为1,它必须是黄色的
I have a matrix, X, in which I want to plot it using the kmeans function. What I would like: If row has a value of 1 in column 4 I would like it to be square shaped If the row has a value of 2 in column 4 I would like it + shaped BUT If the row has a value of 0 in column 5 it must be blue and if the row has a vale of 1 in column 5 it must be yellow
(您不需要使用这些确切的颜色和形状,我只想区分这些。)我试过这个,它没有工作:
(You don't need to use these exact colors and shapes, I just want to distinguish these.) I tried this and it did not work:
plot(X(idx==2,1),X(idx==2,2),X(:,4)==1,'k.');
谢谢!
推荐答案
根据我建议这个嵌套逻辑:
Based on the example on the kmeans
documentation page I propose this "nested" logic:
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)
$ b b
上面的代码是一个最小的工作示例,因为统计工具箱是可用的。
关键功能是绘图的嵌套逻辑。例如:
The above code is a minimal working example, given that the statistics toolbox is available.
The key feature is the nested logic for the plotting. For example:
X(X(idx==1,5)==0,1)
内部 X(idx == 1,5)
X(:,5)
为 idx == 1
。从那些,只有 0
的值被考虑: X(X(...)== 0,1)
。基于问题中的逻辑,这应该是一个蓝色方块: bs
。
您有四种情况,因此有四条曲线。
The inner X(idx==1,5)
selects those values of X(:,5)
for which idx==1
. From those, only values which are 0
are considered: X(X(...)==0,1)
. Based on the logic in the question, this should be a blue square: bs
.
You have four cases, hence there are four additional plot lines.
这篇关于如何绘制这个? MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!