MATLAB: Kmeans algorithm for data

clusteringk-means

Hi everyone.
I need help, checking internet I found the following code;
in= load('C:\Users\Profesional\Desktop\datos_RBR.txt'); % *174x3 double input parameters*
x= (in-min(in(:))) ./ (max(in(:)))-min(in(:)); %normalize values
N= 4; %number of clusters
iter= 100; %number of iterations
centros= x( ceil(rand(N,1)*size(x,1)) ,:); % ubication of the centers (random)
DAL = zeros(size(x,1),N+2);
CV = '+r+b+c+m+k+yorobocomokoysrsbscsmsksy'; % Color Vector
for n = 1:iter
for i = 1:size(x,1)
for j = 1:N
DAL(i,j) = norm(x(i,:) - centros(j,:)); % Menor distancia
end
[Distance CN] = min(DAL(i,1:N)); % 1:K are Distance from Cluster Centers 1:K
DAL(i,N+1) = CN; % K+1 etiquetación
DAL(i,N+2) = Distance; % K+2 distancia minima
end
for i = 1:N
A = (DAL(:,N+1) == i); % Cluster K Points
centros(i,:) = mean(x(A,:)); % media de cada columna para centros
if sum(isnan(centros(:))) ~= 0 %eliminamos posiles valores NAN delete NAN valors
NC = find(isnan(centros(:,1)) == 1); % encontrar centros minimos
for Ind = 1:size(NC,1)
centros(NC(Ind),:) = x(randi(size(x,1)),:);
end
end
end
end
% Plot
clf
figure(1)
hold on
for i = 1:N
PT = x(DAL(:,N+1) == i,:); % encontrar los puntos de cada neurona // find points
plot(PT(:,1),PT(:,2),CV(2*i-1:2*i),'LineWidth',2); % Plot groups
plot(centros(:,1),centros(:,2),'*k','LineWidth',7); % Plotear centers
end
hold off
grid on
pause(0.1)
Well, this one allows me set the number of cluster (groups),number of iterations and clust the input values which is really good, however, I don't know how to view the number of values or points that containes each cluster (group), I would appreciate your help.
I've added two notes one with the code and the other one with the input values (datos_RBR.txt) as well

Best Answer

data = importdata('data.txt') ;
x = data(:,1) ;
y = data(:,2) ;
z = data(:,3) ;
N = 3 ; % number of groups
idx = kmeans(data,N) ; % this gives you indices to which group the points belong
figure
hold on
for i = 1:N
plot3(x(idx==i),y(idx==i),z(idx==i),'.')
end