MATLAB: What is the best method to generate random points

bestrandom

Hi,
Suppose i have a set of points such as (x,y) vector:
1.3300 8.8900
0.8800 7.0200
0.7500 4.9800
2.2200 4.3500
3.1300 1.9200
1.7400 1.3700
1.8900 0.7700
4.0100 0.3100
6.0800 1.3400
7.3400 1.3800
8.4500 0.6900
8.6000 0.5300
9.2700 1.4900
8.9900 2.4500
7.6700 4.1700
7.5500 5.7900
9.3900 6.4400
8.9300 7.0000
9.2000 8.6900
9.4600 9.3600
8.8600 8.7400
7.2300 7.0500
5.8900 8.0600
5.0000 9.0000
2.8300 9.8800
what is the best method/function to generate random points within?

Best Answer

Try this:
xy = [...
1.3300 8.8900
0.8800 7.0200
0.7500 4.9800
2.2200 4.3500
3.1300 1.9200
1.7400 1.3700
1.8900 0.7700
4.0100 0.3100
6.0800 1.3400
7.3400 1.3800
8.4500 0.6900
8.6000 0.5300
9.2700 1.4900
8.9900 2.4500
7.6700 4.1700
7.5500 5.7900
9.3900 6.4400
8.9300 7.0000
9.2000 8.6900
9.4600 9.3600
8.8600 8.7400
7.2300 7.0500
5.8900 8.0600
5.0000 9.0000
2.8300 9.8800];
x = xy(:, 1);
y = xy(:, 2);
% Show the points
% subplot(2, 2, 1);
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
hold on;
% Find ranges
minX = min(x)
maxX = max(x)
minY = min(y)
maxY = max(y)
% Decide on number of points
numRandomPoints = 1000;
pointCount = 0;
while pointCount < numRandomPoints
thisPointX = (maxX - minX) * rand() + minX;
thisPointY = (maxY - minY) * rand() + minY;
if inpolygon(thisPointX, thisPointY, x, y)
plot(thisPointX, thisPointY, 'r.', 'MarkerSize', 14);
pointCount = pointCount + 1;
end
end
Related Question