How to generate say 100 points randomly in a square or rectangle?
조회 수: 16 (최근 30일)
이전 댓글 표시
I want to generate 100 points in a square , which side length is 5 m.
댓글 수: 1
SUSHMA MB
2016년 8월 3일
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
채택된 답변
Azzi Abdelmalek
2013년 9월 18일
편집: Azzi Abdelmalek
2013년 9월 18일
x=rand(1,100)*5
y=rand(1,100)*5
scatter(x,y)
or
a=rand(2,100)*5
scatter(a(1,:),a(2,:))
댓글 수: 5
Tamoor Shafique
2020년 9월 5일
How can I generate 100 random points in randomly in one of the sphere/cube/rectangulare 3D space?
Akhil Govindraj
2022년 3월 9일
That's easy Tamoor Shafique, all you've gotta do is extend this code to the z axis as well and use scatter3() instead of scatter():
x=rand(1,100)*5;
y=rand(1,100)*5;
z=rand(1,100)*5;
scatter3(x,y,z)
추가 답변 (1개)
Image Analyst
2013년 9월 18일
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;
댓글 수: 3
Tamoor Shafique
2020년 9월 5일
How can I generate 100 random points in randomly in one of the sphere/cube/rectangulare 3D space?
Image Analyst
2020년 9월 6일
See the FAQ: https://matlab.fandom.com/wiki/FAQ#How_do_I_create_a_set_of_random_locations_within_a_circle.3F
There is also one for a sphere right below that. Adapt as needed.
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
