Assign 3d points to multiple cluster when their coordinates meet certain conditions
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Yatharth
2024-4-30
Hi LH,
I understand that you're aiming to assign each point in the 3D dataset to a cluster based on specific conditions, as outlined in your question and provided code.
After setting initial centroids, consider using this code snippet instead:
threshold_x = 1.5;
threshold_y = 1.5;
threshold_z = 1.5;
% Initialize the cluster assignment matrix
cluster_assignments = zeros(size(X,1), 4);
for i = 1:size(X,1)
for j = 1:NoCluster
% Calculate the distance between the point and the centroid
dist_x = abs(X(i,1) - initial_centroids(j,1));
dist_y = abs(X(i,2) - initial_centroids(j,2));
dist_z = abs(X(i,3) - initial_centroids(j,3));
% Check if the point meets the conditions for all coordinates
if dist_x <= threshold_x && dist_y <= threshold_y && dist_z <= threshold_z
% Assign the point to this cluster
cluster_assignments(i,:) = [X(i,:), j];
break; % Break the loop once the point is assigned to a cluster
end
end
end
% Display the final cluster assignments
disp(cluster_assignments);
I hope this is what you were looking for.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cluster Analysis and Anomaly Detection 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!