Info

此问题已关闭。 请重新打开它进行编辑或回答。

I want to generate spherical from three given arrays, please help me out.

1 次查看(过去 30 天)
SHUBHAM SINGH CHOUDHARY
关闭: MATLAB Answer Bot ,2021-8-20
X=(0:0.02:1);
Y=(0:0.02:1);
[x,y] = meshgrid(X,Y);
%z=rand(size(x))*1000;
%z=1e6*ones(size(x));
z=ones(size(x));
x0=mean(X);
y0=mean(Y);
b_center1 = [0.2,0.8];
b_center2 = [0.6,0.2];
b_center3 = [0.4,0.55];
r1=0.1*max(X);
r2=0.1*max(X);
r3=0.1*max(X);
for i=1:length(X)
for j=1:length(Y)
dist = sph2cart([[X(i),Y(j)];b_center1]);
if (dist < r1)
z(i,j)=1;
end
dist = sph2cart([[X(i),Y(j)];b_center2]);
if (dist < r2)
z(i,j)=1;
end
dist = sph2cart([[X(i),Y(j)];b_center3]);
if (dist < r3)
z(i,j)=1;
end
end
end
surf(x,y,z)

回答(1 个)

Constantino Carlos Reyes-Aldasoro
We would need a better explanation of exactly what you want, but I think that the main issues are: 1) you are using sph2cart instead of cart2sph and 2) you are not treating your data as matrices. In general, in Matlab you should not use for loops when you can treat everything as a matrix. Try the following code:
X=(0:0.02:1);
Y=(0:0.02:1);
[x,y] = meshgrid(X,Y);
z=repmat(permute(-1:0.02:1,[ 3 1 2]),51,51);
[azimuth,elevation,r] = cart2sph(x,y,z);
Notice that the z was created in a single line, no need to loop. Then with x,y,z you can convert coordinates and then you have your spherical space.
h1=patch(isosurface(r,0.5));
view(60,10)
lighting phong
camlight right
h1.FaceColor='r';
h1.EdgeColor='none'
Hope this helps. Try for your problem. If this solves your problem, please accept the answer. I
  2 个评论
SHUBHAM SINGH CHOUDHARY
I want to generate spherical clusters like this with three arrays
for above clusters I have used code like this
rvals = 2*rand(2000,1)-1;
elevation = asin(rvals);
azimuth = 2*pi*rand(2000,1);
radii = 3*(rand(2000,1).^(1/3));
[x,y,z] = sph2cart(azimuth,elevation,radii);
figure
plot3(x+8,y+8,z+8,'.');
hold on;
plot3(x+4,y+4,z+4, '.')
hold on;
plot3(x,y,z,'.')
axis equal

Community Treasure Hunt

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

Start Hunting!

Translated by