random Distribution Points inside rectangle

10 ビュー (過去 30 日間)
MOTHANA LAFTA
MOTHANA LAFTA 2017 年 2 月 5 日
編集済み: MOTHANA LAFTA 2017 年 2 月 6 日
hi every one how can i distribute randomly a number of points inside rectangle and every point has x,y ex: px(1)=100; py(1)=-100; and so forth where the rectangle code as shown bellow:-
x=-1100;
y=-1100;
rectangle('position',[x,y,2200,2200],'LineWidth',2,'LineStyle','--');

採用された回答

Image Analyst
Image Analyst 2017 年 2 月 5 日
Try this:
numPoints = 1000;
width = 2200;
x = -1100
y = -1100;
rectangle('Position', [x, y, width, width],'LineWidth',2,'LineStyle','--');
grid on;
xRandom = width * rand(1, numPoints) - width / 2;
yRandom = width * rand(1, numPoints) - width / 2;
hold on;
plot(xRandom, yRandom, 'r.', 'MarkerSize', 10);
title('Random Points Inside a Rectangle', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
axis equal;
% Put axis at origin
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
  1 件のコメント
MOTHANA LAFTA
MOTHANA LAFTA 2017 年 2 月 6 日
編集済み: MOTHANA LAFTA 2017 年 2 月 6 日
thanks dear i wish you all the best

サインインしてコメントする。

その他の回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2017 年 2 月 5 日
Have a look at the documentation of rand. That function gives you uniformly distributed random numbers between 0 and 1 (have forgotten about what the limits are, but you'll see). Then think about what happens if you add and multiply those numbers with some fixed constants, then think about what happens if you combine 2 such numbers into a 2-d vector. If you want some other distribution of the points inside your rectangle or want the points to have some covariance structure you'll have to work those out as well.
HTH

Community Treasure Hunt

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

Start Hunting!

Translated by