How can I generate spheres from points in MATLAB?
matlab
Solution
you can use builtin `sphere`, multiply by radius and add center coordinates. to plot them all at once you can use `cellfun`:
% number of spheres
n = 10;
% random xyz center points
xyz = rand(n,3)*10;
% random radius
r = rand(n,1);
% generate unit sphere (radius=1, center=[0,0,0])
[X,Y,Z] = sphere;
% plot function for all spheres
plotfun = @(c,r) surf(X*r + c(1),Y*r + c(2),Z*r + c(3));
% generate figure with "hold on"
figure;
hold on;
axis equal;
grid on;
% plot all spheres
h = cellfun(plotfun,num2cell(xyz,2),num2cell(r),'UniformOutput',0);
if you want the spheres similar to your desired out put you can add some graphical properties to `surf` and add a `light` object:
plotfun = @(c,r) surf(x*r + c(1),y*r + c(2),z*r + c(3),...
'FaceColor',.7*[1 1 1],'EdgeColor','none',...
'FaceLighting','gouraud','AmbientStrength',0.5);
light('Position',[-1 0 0]);
Problem
I have generated random 3D points in MATLAB. The length of the array of the points changes at each run. I want to turn these points into spheres. However, I am not successful yet. Scatter plot of my points are like this: Each point is represented with x,y and z. Now, I want to use that x,y and z as a center point and generate spheres with r radius? How can I do that? To give you an idea, an example picture to show what I expect to generate: