how can I plot random points in a random area

Hi everyone,
Thank you all who are already trying to help me and appologies for keep asking questions.
In this thread I want to learn from experts following:
1.How to Plot 'n' number of points randomly in one of the following 3D spaces/regions i.e. (cube/3d Rectangular/Speherical).
2.the code should randomly pick one of the spaces each timeand then plot 'n' random points uniformly in that space.
3.once plotted how can I obtain the location information of each point centrally? May be at an additional point/gateway at the origin.
I know there are many questions within one question but it is important to explain to someone who is trying to help you what exactly you wish to do. Any help for even a partial code would be appretiated.
I have already accomplished how to distribute random points on a 3d rectangular plot but the point which I am unsure about how to do the same when the dimentions of plot are to be randomly picked from a set of polar and rectangular coordinates.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 5 日

1 投票

Here is an example to plot points in spherical coordinates
n = 1000; % number of points
az = rand(1, n)*2*pi; % azimuth angle in range [0, 2*pi]
elev = rand(1, n)*2*pi - pi; % elevation angle in range [-pi, pi]
r = rand(1, n)*2; % radius in range [0, 3]
[x, y, z] = sph2cart(az, elev, r);
plot3(x, y, z, '+')

9 件のコメント

Tamoor Shafique
Tamoor Shafique 2020 年 9 月 9 日
Thank you.
This is a good example of how to plot random points in spherical coordinates however, I want my code to randomly pick one of the coordinates with a boundary in cube, 3D rectangular or spherical shape.
Any idea on that one? How do i choose random shapes with such boundaries and then plot random points in that shape?
Walter Roberson
Walter Roberson 2020 年 9 月 9 日
If the cuboid is aligned with the axes, use the general technique
minimum_coordinate + rand(NUMBER_OF_POINTS) * (maximum_coordinate - minimum_coordinate)
and repeat for all three axes.
If the cuboid is not aligned with the axes, then typically the easiest approach is to generate 3 random sets of coordinates using rand() and multiply them by a rotation-and-translation matrix. maketform() can help generate those matrices. You do, though, need to figure out what the alignment of the cuboid is to do this.
The technique is the same for cube or cuboid, other than that the multiplying factors are the same for the three directions for cube but are different for the three directions for cuboid. cube is a subset of cuboid, so there is little point in implementing different code for the two.
Ameer Hamza
Ameer Hamza 2020 年 9 月 9 日
Do you want to select a shape too randomly too? Check the following code, which decides which shape to draw based on the value of the random variable
m = randi([1 3]); % type of shape, (m=1 => cube, m=2 => rectangular, m=3 => spherical)
n = 1000; % number of points
switch m
case 1
len = 1; % length of each side
X = rand(n, 3)*len - len/2;
x = X(:, 1);
y = X(:, 2);
z = X(:, 3);
case 2
len = 1;
width = 2;
height = 5;
x = rand(n, 1)*len - len/2;
y = rand(n, 1)*width - width/2;
z = rand(n, 1)*height - height/2;
case 3
az = rand(1, n)*2*pi; % azimuth angle in range [0, 2*pi]
elev = rand(1, n)*2*pi - pi; % elevation angle in range [-pi, pi]
r = rand(1, n)*2; % radius in range [0, 3]
[x, y, z] = sph2cart(az, elev, r);
end
plot3(x, y, z, '+')
Walter Roberson
Walter Roberson 2020 年 9 月 9 日
Ameer, that random distribution probably needs to be modified.
If you take
r = sqrt(x.^2 + y.^2 + z.^2);
histogram(r,25)
you will see a pretty flat histogram -- the number of points close to radius 2 is fairly similar to the number of poitns at radius 1/4. But if you distribute points randomly in 3-space with equal distribution by volume, then because there is more volume at larger radius, you would expect more points at larger radius. You would, in fact, expect the number of points to increase with the cube of the radius.
Ameer Hamza
Ameer Hamza 2020 年 9 月 9 日
In case if a uniform distribution is needed, it might be easier to generate uniformly distributed points in a cube and just discard the points outside the spherical region.
R = 1; % radius of sphere
X = rand(n, 3)*2*R - R;
idx = vecnorm(X, 2, 2) < R;
X = X(idx, :);
x = X(:, 1);
y = X(:, 2);
z = X(:, 3);
Tamoor Shafique
Tamoor Shafique 2020 年 9 月 9 日
Thank you @Ameer Hamza and @Walter Roberson for your help. This one almost achieved. I have used this in my own code but the commands worked decently well.
Thank you
Walter Roberson
Walter Roberson 2020 年 9 月 9 日
There are web pages about directly generating on the inside of a sphere without rejection.
Rejection from a cube is about 54% efficient.
Tamoor Shafique
Tamoor Shafique 2020 年 9 月 9 日
You are right I have to modify the code for generating points inside a spherical boundary but switch command helped me to choose one of the three random cases

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

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

製品

質問済み:

2020 年 9 月 5 日

コメント済み:

2020 年 9 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by