Centroid plotting k-means

1 次查看(过去 30 天)
Gabriel
Gabriel 2016-2-28
编辑: Ced 2016-3-6
Hi guys, I am currently working on a k-means problem. I am struggling to plot the Centroid provided. I currently get the following error messages: Error using scatter3 (line 45) C must be a single color, a vector the same length as X, or an M-by-3 matrix.
Error in import_excelv4 (line 86) scatter3(U(:,1), U(:,2), U(:,3), 100, clr,'Marker','o','LineWidth',3)
My code is as shown bellow and my data is attached to this question
if true
% %%
%importar dados do excel
imp = xlsread('Academia.xlsx');
%% Loop Kmeans K clusters
k=20
CH=zeros(1,k); SH=zeros(1,k); DB=zeros(1,k); SUB=zeros(1,k); RR=zeros(80,k); % eva = evalclusters(x,clust,criterion) for i=1:k [idx,C]=kmeans(imp,i,'MaxIter',1000000); %C=centroides % Como pegar o valor de idx ? RR(:,i)=idx T{i}=C eva = evalclusters(imp, idx, 'CalinskiHarabasz') CH(1,i)=eva.CriterionValues eva2 = evalclusters(imp, idx, 'Silhouette') SH(1,i)=eva2.CriterionValues eva3 = evalclusters(imp, idx, 'DaviesBouldin') DB(1,i)=eva3.CriterionValues end
%% Calculos das metricas CH(1,1)=0 % % Descobrir as diferenças entre os valores de CH while i>1 SUB(1,i)=CH(1,i)-CH(1,i-1); i=i-1; end SUB(1,1)=0
%Achar o pulo maximo entre os clusters para o valor de CH: [V,N]=max(SUB) % Por algum motivo está pulando a célula vazia e fornecendo valor incorreto
SH
%SHF=cell2mat(SH)
[V2,N2]=max(SH) %Valor mais próximo de 1
DB
%DBF=cell2mat(DB)
[V3,N3]=min(DB) %Valor mais próximo de 0
%%Melhor qtd de clusters
if (N==N2) && (N2==N3) disp('CH=SH=DB') N N2 N3 i=N; Z=RR(:,i) y=xlswrite('Academia_target.xlsx',Z,'D2:D81'); U=T{i} elseif N2==N3 disp('SH=DB') elseif N==N3 disp('CH=DB') elseif N==N2 disp('CH=SH') else disp('Todas as métricas forneceram valores diferentes') end
%% Plotar os dados:
[numInst,numDims] = size(imp);
%# show points and clusters (color-coded)
clr = lines(k);
figure,
scatter3(imp(:,1), imp(:,2), imp(:,3), 36, clr(Z,:), 'Marker','.')
hold on
scatter3(U(:,1), U(:,2), U(:,3), 100, clr,'Marker','o','LineWidth',3)
hold on
view(3), axis vis3d, box on, rotate3d on
hold on
xlabel('flexibilidade'), ylabel('velocidade'), zlabel('forca')
hold off
end
I dont understand the error messages since I am using U as a matrix for the centroids and only a single color.
Can someone please help me ?
Thanks in Advance !

回答(1 个)

Ced
Ced 2016-3-6
编辑:Ced 2016-3-6
Hi
It looks like you got a bit mixed up in your indices:
clr = lines(k);
creates a matrix with k colors, but you only have i <= k centroids (Since U = T{i} with i == N <= k).
Using
clr = lines(i);
solves the issue.

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by