Assign 3d points to multiple cluster when their coordinates meet certain conditions
Show older comments
Hi all,
I am trying to assign each point of the 3D dataset I have to the "correct" cluster based on certain conditions. The code below shows the 3D dataset I have with 15 data points. Here I have 4 clusters to assign these 15 points to. The challenge here is the following:
- I want to test the distance between the x cooridnate of the point and the x cooridnate of the initial centroid and the distance should be equal or less some value. The same goes for the y cooridnate.
- If the previous condition is met, test the z coordinate in the same way.
- If the previous two conditions are met, assign this point to the "correct" cluster. I think the final index matrix should be 15x4 where the rows corrspond to the number of points, the first column is the x coordinates, the second column is the y coorindtaes, the third column is the z coordinates, and the fourth clumn correspnds to the cluster index.
clear all;
close all;
%3D dataset
X = [0.6820 -0.4255 0.4255
0.6820 0.8088 0.5881
0.6820 -0.9337 -0.3580
0.6820 0.5513 0.8343
0.6820 -0.4142 0.4142
0.6820 0.5281 0.8492
0.6820 -0.9234 -0.3838
0.6820 -0.2950 0.2950
0.6820 -0.3111 0.3111
0.6820 -0.7517 -0.6595
0.6820 0.3220 0.9467
0.6820 -0.3805 0.3805
0.6820 0.6108 0.7918
0.6820 -0.2954 0.2954
0.6820 0.9530 0.3029];
%number of lcusters
NoCluster = 4;
%initial random centroids
xmin = 1 ; xmax = 4;
ymin = -1; ymax = 1;
zmin = -1; zmax = 1;
initial_centroids = [xmin+(xmax-xmin).*rand(NoCluster,1)...
ymin+(ymax-ymin).*rand(NoCluster,1)...
zmin+(zmax-zmin).*rand(NoCluster,1)];
%calculate the distance betweene each point and each centorid
%go through all points
for isp = 1:size(X,1)
%go through all clusters
for ic = 1:size(initial_centroids,1)
%measure the distance between each to each of the
%preivously determined centroids
Dist_x(isp,ic) = abs(X(isp,1)-initial_centroids(ic,1));
Dist_y(isp,ic) = abs(X(isp,2)-initial_centroids(ic,2));
Dist_z(isp,ic) = abs(X(isp,3)-initial_centroids(ic,3));
end
end
%go through all calculated distances for testing
for idist = 1:size(Dist_x,1)
%go throguh all clusters
for icc = 1:size(initial_centroids,1)
%test x and y
if Dist_x(idist,icc) <= 0.01 && ...
Dist_y(idist,icc) <= 0.01
%if the previous condiiton is met, test the
%corresponding z point
if Dist_z(idist,icc) <= 0.01
%if the previous two conditions are met,
%assign this point to the current cluster and save
%its cluster index
[~ , ~ , ~ , cluster_indices] = ; %what to write here?
end
end
end
end
Any help would be appreciated.
Thanks.
Accepted Answer
More Answers (0)
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!