how to save number of organization for each cluster in k mean clustering.

1 view (last 30 days)
i am making 4 clusters using k mean clustering. i want to get and save the indexes of the data which are a part of each cluster how can i?
%% matlab code
clc
clear all
close all
radius=15;
X0=0; % x-coordinate of Centre of the circle
Y0=0; % y-coordinate of Centre of circle
% Users
K=4;
t = 2*pi*rand(K,1);
R = radius*sqrt(rand(K,1));
x_u = X0 + R.*cos(t); % X-coordinate of the Users
y_u = Y0 + R.*sin(t); % Y-coordinate of the Users
X3=[-6.189; -3.251; 2.926; 6.534; 10.79; 5.203; -4.465; 1.42; -8.66; -0.8748; 6.534 ; 11.251 ;0.7298; 3; -5.23; 11.86; x_u] ;
Y3=[ 1.393; 10.42; 6.639; 5.821; 8.425; 2.924; 1.139; 0.7508; -7.917; -6.928; -10.01; -7.03; 12.48; -1.497; -6.139; -3.23; y_u ];
x_b=[0.7298; 3; -5.23; 11.86];
y_b=[12.48; -1.497; -6.139; -3.23];
P = [X3 Y3] ; % points
G = 4 ; % number of groups
C = [x_u+x_b/2 y_u+y_b/2] ; % centroids
idx = kmeans(P,G,'MaxIter',10,'Start',C);
gscatter(P(:,1),P(:,2),idx)
i want to store that which index value is a part of particular cluster e.g X3(2) is a part of which cluster. i want to store index 2 basically and simarly for all values
  6 Comments
Maimoona Asad
Maimoona Asad on 31 Mar 2022
Edited: Maimoona Asad on 31 Mar 2022
how can i access the location points are accessible but i want there indexes e.g the selected point in a cluster is at which index position of original data before clustering

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Mar 2022
radius=15;
X0=0; % x-coordinate of Centre of the circle
Y0=0; % y-coordinate of Centre of circle
% Users
K=4;
t = 2*pi*rand(K,1);
R = radius*sqrt(rand(K,1));
x_u = X0 + R.*cos(t); % X-coordinate of the Users
y_u = Y0 + R.*sin(t); % Y-coordinate of the Users
X3=[-6.189; -3.251; 2.926; 6.534; 10.79; 5.203; -4.465; 1.42; -8.66; -0.8748; 6.534 ; 11.251 ;0.7298; 3; -5.23; 11.86; x_u] ;
Y3=[ 1.393; 10.42; 6.639; 5.821; 8.425; 2.924; 1.139; 0.7508; -7.917; -6.928; -10.01; -7.03; 12.48; -1.497; -6.139; -3.23; y_u ];
x_b=[0.7298; 3; -5.23; 11.86];
y_b=[12.48; -1.497; -6.139; -3.23];
P = [X3 Y3] ; % points
G = 4 ; % number of groups
C = [x_u+x_b/2 y_u+y_b/2] ; % centroids
[idx, C] = kmeans(P,G,'MaxIter',10,'Start',C);
P_by_cluster = splitapply(@(varargin) varargin, P, idx);
C
C = 4×2
-1.6719 -0.3739 9.8817 -6.7567 -5.9945 -5.8297 2.8697 7.9945
P_by_cluster
P_by_cluster = 4×1 cell array
{5×2 double} {3×2 double} {5×2 double} {7×2 double}

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!