MATLAB: How to set concentric rectangle dataset?

toydata

TIM图片20191230235237.pngI want that the number of each color data point can be changed

Best Answer

Try this:
% Define parameters.
numPoints = 1000000; % Something way bigger than you think you'll need.
n1 = 200;
n2 = 300;
n3 = 400;
% Generate numPoints (x,y) locations.
x = 3 * rand(numPoints, 2) - 1.5;
y = 3 * rand(numPoints, 2) - 1.5;
% plot(x, y, '.');
%-----------------------------------------------------------


% Get the inner zone points and plot them.
zone1 = (abs(x) < 0.5) & (abs(y) < 0.5); % Inner zone
zone1i = find(zone1, n1); % Get the actual indexes for the first n1 points.
x1 = x(zone1i);
y1 = y(zone1i);
hold on;
plot(x1, y1, 'ro');
%-----------------------------------------------------------
% Get the outer zone points and plot them.
zone3 = ~((abs(x) < 1) & (abs(y) < 1)); % Outer zone
zone3i = find(zone3, n3); % Get the actual indexes for the first n3 points.
x3 = x(zone3i);
y3 = y(zone3i);
hold on;
plot(x3, y3, 'b^');
%-----------------------------------------------------------
% Get the ring zone points and plot them.
zone2 = ~(zone3 | zone1); % Ring zone is everything except those in zone 1 or zone 3.
zone2i = find(zone2, n2); % Get the actual indexes for the first n2 points.
x2 = x(zone2i);
y2 = y(zone2i);
hold on;
plot(x2, y2, 'g+');
grid on;
axis equal;
0001 Screenshot.png