how to set concentric rectangle dataset?

4 次查看(过去 30 天)
TIM图片20191230235237.pngI want that the number of each color data point can be changed
  10 个评论
z cy
z cy 2019-12-31
n1 = 500 ;
noise1 = 0.02;
r = unifrnd(-0.9,0.9,[1,n1]);
t = unifrnd(-0.5,0.5,[1,n1]);
x = r + noise1*randn(1,n1);
y = t + noise1*randn(1,n1);
data1 = [x;y];
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);
data2 = [x;y];
r = unifrnd(-2.9,2.9,[1,n1*4]);
t = unifrnd(-2.5,2.5,[1,n1*4]);
b = [r;t];
b(:,(abs(b(1,:)) <=2.15)+(abs(b(2,:))<1.6) ==2) =[];
n3 = size(b,2);
x = b(1,:) + noise1*randn(1,n3);
y = b(2,:) + noise1*randn(1,n3);
data3 = [x;y];
X_train = [data1,data2,data3];
Y_train = [ones(1,n1),2*ones(1,n2),3*ones(1,n3)];
figure
plot(data1(1,:), data1(2,:), 'ro',data2(1,:), data2(2,:), 'g+',data3(1,:), data3(2,:), 'b^','linewidth',1.5);
Adam Danz
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
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

更多回答(1 个)

Image Analyst
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 个评论
z cy
z cy 2019-12-31
In real, n1 and n2 have no relationship. I just dont know how to set this plot directly. So that I use delete
b(:,(abs(b(1,:)) <=1.15)+(abs(b(2,:))<0.65) ==2) =[];
n2 = size(b,2);
However,n2 is a number which can not directly to control. What I need is to directly to set n2, not use deleted the data and to get n2.
Image Analyst
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 CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by