what is the best method to generate random points
    4 views (last 30 days)
  
       Show older comments
    
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?
1 Comment
  hmi amid
      
 on 1 May 2017
				Do you mean random points that are already in this matrix (x,y)? Or new random points in the range of this matrix? For the first meaning you can use :
Suppose your set of points is in V
i=ceil(rand(1)*length(V));
x_random=V(i,1);y_random=V(i,2);
For the second case:
x_random=min(V(:,1))+rand(1)*(max(V(:,1))-min(V(:,1)));
y_random=min(V(:,2))+rand(1)*(max(V(:,2))-min(V(:,2)));
Best, Amid.
Accepted Answer
  Image Analyst
      
      
 on 1 May 2017
        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

0 Comments
More Answers (0)
See Also
Categories
				Find more on Random Number Generation in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

