如何在MATLAB中使用radius1=40
和radius2 =100
生成随机的两个同心球体合成数据,并使用一个变量以*.mat
格式保存该数据。(1000*3 double)
?另外,如何使用2种颜色在3D中绘制此数据:红色和蓝色?
我的代码:
rng(0,'twister');
rvals = 2*rand(1000,1)-1;
elevation = asin(rvals);
azimuth = 2*pi*rand(1000,1);
radii = 3*(rand(1000,1).^(1/3));
[x,y,z] = sph2cart(azimuth,elevation,radii);
data=[x,y,z];
figure
plot3(x,y,z,'.');
axis equal
预期输出:
答案 0 :(得分:1)
简单,球坐标样本
phi = 2 * Pi * U(0,1)
cos(theta) = 2 * U(0,1) - 1
r = r_min * U(0,1)^(1/3) (r_max - r_min)
sin(theta) = sqrt(1-cos^2(theta))
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)
答案 1 :(得分:1)
如果你想要一个固定的半径,那么为它们分配一个常数值(而不是一个随机值)并保留方向角的随机数(azimuth/elevation
或theta/phi
取决于符号)。
此代码:
rng(0,'twister');
nptSet = 500 ; r1 = 40 ; r2 = 100 ; %// your constraints
%// first data set (r1=40)
r1 = zeros(nptSet,1)+r1 ; %// assign radius (fixed)
azi1 = rand( size(r1) ) * 2*pi ; %// random azimuth [ 0 2pi]
elev1 = (rand(size(r1)) .* pi)-pi/2 ; %// random elevation [-pi/2 pi/2]
%// second data set (r2=100)
r2 = zeros(nptSet,1)+r2 ; %// assign radius (fixed)
azi2 = rand( size(r2) ) * 2*pi ; %// random azimuth [ 0 2pi]
elev2 = (rand(size(r2)) .* pi)-pi/2 ; %// random elevation [-pi/2 pi/2]
%// convert to cartesian
[x1,y1,z1] = sph2cart(azi1,elev1,r1);
[x2,y2,z2] = sph2cart(azi2,elev2,r2);
%// display and refine
figure ; hold on
plot3(x1,y1,z1,'or','MarkerSize',2);
plot3(x2,y2,z2,'+b');
xlabel('x') ; ylabel('y') ; zlabel('z')
axis equal ; grid off ; view(50,30)
会得到你那个数字: