如何生成随机的两个同心球体合成数据? [英] How to generate random two concentric spheres synthetic data?

查看:33
本文介绍了如何生成随机的两个同心球体合成数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MATLAB中生成radius1=40radius2 =100的随机两个同心球合成数据,并以*.mat 带有一个变量.(1000*3 double)?另外,如何用 2 种颜色在 3D 中绘制此数据:红色和蓝色?

How to generate random two concentric spheres synthetic data with radius1=40 and radius2 =100 in MATLAB and save that data in format *.mat with one variable.(1000*3 double)? Also, how to plot this data in 3D with 2 colors: red and blue?

我的代码:

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

预期输出:

推荐答案

如果你想要一个固定的半径,那么为它们分配一个常数值(而不是一个随机值)并保留随机数方向角(azimuth/elevationtheta/phi 取决于符号).

If you want a fixed radius then assign them a constant value (instead of a random value) and keep the random numbers for the orientation angles (azimuth/elevation or theta/phi depending on notation).

此代码:

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)

会给你那个数字:

这篇关于如何生成随机的两个同心球体合成数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆