MATLAB Answers

0

How to generate say 100 points randomly in a square or rectangle?

Asked by GA optimization on 18 Sep 2013
Latest activity Commented on by SUSHMA MB
on 3 Aug 2016

I want to generate 100 points in a square , which side length is 5 m.

  1 Comment

How to generate say 10 out of 100 random points on a straight line joining two points? I have two points let, (x1,y1) and (x2,y2). These two points are joined by a straight line. Now i want to generate, 10 points out of 100 random points on the straight line. Let the boundary be a square, with side length 50m

Products

No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek
on 18 Sep 2013
Edited by Azzi Abdelmalek
on 18 Sep 2013
 Accepted answer

x=rand(1,100)*5
y=rand(1,100)*5
scatter(x,y)

or

a=rand(2,100)*5
scatter(a(1,:),a(2,:))

  2 Comments

Thanks Azzi.

How to generate say 10 out of 100 random points on a straight line joining two points? I have two points let, (x1,y1) and (x2,y2). These two points are joined by a straight line. Now i want to generate, 10 points out of 100 random points on the straight line. Let the boundary be a square, with side length 50m


Answer by Image Analyst
on 18 Sep 2013

Here's an alternate interpretation, if you want 100 locations in a matrix set to some value, such as 1.

% Randomly place a value of 1 at 100 locations.
m=10; % Whatever
% Make a "canvass" of all zeroes.
theArray = zeros(5*m);
% Get 100 linear indices randomly located
linearIndices = randperm(numel(theArray), 100);
% Make those 100 locations have a value of 1:
theArray(linearIndices) = 1;

  1 Comment

How to generate say 10 out of 100 random points on a straight line joining two points? I have two points let, (x1,y1) and (x2,y2). These two points are joined by a straight line. Now i want to generate, 10 points out of 100 random points on the straight line. Let the boundary be a square, with side length 50m


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!