how to generate random two concentric spheres synthetic data?

3 views (last 30 days)
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
The output should look like this photo.

Accepted Answer

Walter Roberson
Walter Roberson on 11 May 2015
rng(0,'twister');
rvals = 2*rand(500,2)-1;
elevation = asin(rvals);
azimuth = 2*pi*rand(500,2);
radii = 3*(rand(500,2).^(1/3));
[x,y,z] = sph2cart(azimuth,elevation,radii);
allxyz = [x(:,1)*radius1, y(:,1)*radius1, z(:,1)*radius1;
x(:,2)*radius2, y(:,2)*radius2, z(:,2)*radius2];
Now save allxyz: it is your requested one variable.(1000*3 double)
To plot,
figure
plot3(allxyz(1:end/2,1),allxyz(1:end/2,2),allxyz(1:end/2,3),'r.');
hold on
plot3(allxyz(end/2+1:end,1),allxyz(end/2+1:end,2),allxyz(end/2+1:end,3),'b.');
  1 Comment
mohammad ghr
mohammad ghr on 11 May 2015
very thank you. you are my best. how to change 'r' or 'b.' for data point? for example other form.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!