how to set concentric rectangle dataset?
3 次查看(过去 30 天)
显示 更早的评论
![TIM图片20191230235237.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257649/TIM%E5%9B%BE%E7%89%8720191230235237.png)
10 个评论
Adam Danz
2019-12-31
Instead of deleting some of the points, you could replace those coordinates with a new set of random coordinates. That could be set up within a while-loop where you keep replacing values that meet a set criteria until all of the points meet the requirements.
采纳的回答
Image Analyst
2019-12-31
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/257748/0001%20Screenshot.png)
0 个评论
更多回答(1 个)
Image Analyst
2019-12-31
编辑:Image Analyst
2019-12-31
In your code, see how n1 and n2 are passed into randn():
r = unifrnd(-0.9,0.9,[1,n1]);%r= r(randperm(length(r)));
t = unifrnd(-0.5,0.5,[1,n1]);%t= t(randperm(length(t)));
x = r + noise1*randn(1,n1);
y = t + noise1*randn(1,n1);
z = sqrt(noise2)*randn(fea_n,n1);
data1 = [x; y;z];
r = unifrnd(-1.9,1.9,[1,n1*2.5]);
t = unifrnd(-1.5,1.5,[1,n1*2.5]);
b = [r;t];
b(:,(abs(b(1,:)) <=1.15)+(abs(b(2,:))<0.65) ==2) =[];
n2 = size(b,2);
x = b(1,:) + noise1*randn(1,n2);
y = b(2,:) + noise1*randn(1,n2);
z = sqrt(noise2)*randn(fea_n,n2);
data2 = [x;y;z];
So you say "I want that the number of each color data point can be changed" so just change the value of n1 to change the number of points. You didn't show how you assigned it, but you must have assigned it before this code or else the code would not have worked.
2 个评论
Image Analyst
2019-12-31
编辑:Image Analyst
2019-12-31
Do you need to set a specific number for each color of marker, like a specified n1 for red, n2 for green, and n3 for blue? If so, you can just generate a new point and keep it or reject it if it falls in the proper "zone". So I'd just call rand() to get like a few million points. Then get 3 logical vectors to determine which rows are in zone1, zone2, or zone3. Then extract those rows and crop them to just the first n1, n2, or n3 rows. Then plot. See fully worked out code in my other answer on this page.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!