![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979235/image.png)
Find closest sphere in a cluster with random spheres having different diameters
3 views (last 30 days)
Show older comments
Chris Dan
on 26 Apr 2022
Commented: Image Analyst
on 27 Apr 2022
Hi,
I am making clusters of spheres using random number generator. Some of the spheres are connected and some of them are not connected. I want to exclude the not connected ones, so I only get the clusters/connected spheres .I have random spheres having random diameters between 0.1 and 0.5.
here is my code till now
%% Data for Agglomerates
N = 50; % number of spheres
a = 0.1 ; % lowest diameter of sphere
b = 0.5 ; % highest diameter of sphere
Diam = a + (b-a).*rand(N,1);
const = ((1+sqrt(5))/2); % golden ratio bsed on fibonacci sequence
aaa= 0; % minimum center x and y coordinate limit for spheres
bbb= 2 ; % maximum center x and y coordinat limit for sphere
% bbb= N*const; % maximum center x and y coordinat limit for sphere
M=3 ;
Axes= zeros(N,M);
Axes(:,1)=aaa+(bbb-aaa)*rand(N,1);
for k=2:M
aaa=randperm(N);
Axes(:,k)=Axes(aaa,1);
end
Axes_Label ={'X axis','Y axis','Z axis'};
Data_agglo = [Diam Axes];
Data_Label ={'Diameter','X axis','Y axis','Z axis'};
R = Diam ./2;
%% generate mesh
dipS = 0.01;
xmin = min(Data_agglo(:,2)-R);
xmax = max(Data_agglo(:,2)+R);
ymin = min(Data_agglo(:,3)-R);
ymax = max(Data_agglo(:,3)+R);
zmin = min(Data_agglo(:,4)-R);
zmax = max(Data_agglo(:,4)+R);
[Xgrid,Ygrid,Zgrid]= ndgrid((xmin:dipS:xmax)-(xmin+xmax)/2,(ymin:dipS:ymax)-(ymin+ymax)/2,(zmin:dipS:zmax)-(zmin+zmax)/2);
Data_agglo(:,2:4) = Data_agglo(:,2:4) - [(xmin+xmax)/2,(ymin+ymax)/2,(zmin+zmax)/2];
% plot3(Xgrid(:),Ygrid(:), Zgrid(:),'.','MarkerFaceColor','blue');
% hold all
%% get active dipoles
tic
active = false(size(Xgrid));
for i =1:1:size(Data_agglo,1)
active = active | (((Xgrid - Data_agglo(i,2)).^2 + (Ygrid - Data_agglo(i,3)).^2 + (Zgrid - Data_agglo(i,4)).^2) <= R(i).^2);
end
plot3(Xgrid(active),Ygrid(active),Zgrid(active),'o','MarkerFaceColor','red');
toc
active_dip = sum(sum(sum(active)));
volume = active_dip * dipS^3;
Before I generate the mesh, I get the data set called " Data_agglo", which has the diameter and the x,y,z center coordinates of the spheres data, which I later plot.
I have to find a way to exclude the spheres which are not in the cluster before plotting using the data " Data_agglo".
Does any one know?
0 Comments
Accepted Answer
Image Analyst
on 26 Apr 2022
What defines "connected" for a bunch of points? If the centers are within a certain distance of each other? Then you can use dbscan
It groups points into a cluster if there can be a path between the points that is less than some distance. See attached demo.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979235/image.png)
In the above figure on the right you can see the cluster distance parameter of 60 indicated by the red bar and the yellow circle. Now see the points connected/outlined in colored polygons? Those points are all within 60 pixels of at least one other pixel in the cluster. The red points are all NOT in any cluster because they are further than 60 from any other point. It sounds like those are the ones you'd want to exclude from your data set. Learn how dbscan (in the Statistics and Machine Learning Toolbox) can be used in your program.
5 Comments
Image Analyst
on 27 Apr 2022
You can get the distances from a particular point (x0,y0) to all other (x,y) points like
distances = sqrt((x-x0).^2 + (y-y0).^2);
% Find min distance that's not zero (to prevent the particle itself being the closest particle).
minDistance = min(nonzeros(distances))
% Get indexes of coordinates that have this min distance.
indexes = find(distances == minDistance)
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!