MATLAB: How to generate random two concentric spheres synthetic data

random number generator

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.

Best Answer

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.');