How to create randomly placed points within a 3D rectangle?

I want to create random points (solid spheres which i can change the radius) inside a 3D rectangular box, which i can change the dimensions of the box later. I found this code for a randomly place points inside a function and i could like to know if anyone knows how to change it to a rectangle.
% pick plenty of random points
x = -1 + (1+1)*rand(1,500); %x-coordinate of a point
y = -0.6 + (0.6+0.6)*rand(1,500); %y-coordinate of a point
z = -0.8 + (1.2+0.8)*rand(1,500); %z-coordinate of a point
%function defining a heart
F1=(((x.^2) + (9/4).*(y.^2) + z.^2-1).^3+(-x.^2 .* z.^3 -(9/80).*y.^2.*z.^3));
ind = F1<0; % index for points inside the isosurface
x = x(ind); x = x(1:100); % pick 100 of those points
y = y(ind); y = y(1:100);
z = z(ind); z = z(1:100);
scatter3(x, y, z, 'MarkerFaceColor','b','MarkerEdgeColor','b');
hold off

 採用された回答

Alan Stevens
Alan Stevens 2020 年 8 月 28 日

0 投票

Do you mean something like this:
% length, width and height of box
L = 3; W = 2; H = 1;
% pick plenty of random points
x = L*rand(1,100); %x-coordinate of a point
y = W*rand(1,100); %y-coordinate of a point
z = H*rand(1,100); %z-coordinate of a point
scatter3(x, y, z, 'MarkerFaceColor','b','MarkerEdgeColor','b');
box

3 件のコメント

Don jaya
Don jaya 2020 年 8 月 28 日
Is it possible to change the size of the spheres? like by changing the radius/diameter or the shperes.
Alan Stevens
Alan Stevens 2020 年 8 月 28 日
Yes, try:
% length, width and height of box
L = 3; W = 2; H = 1;
% pick plenty of random points
x = L*rand(1,100); %x-coordinate of a point
y = W*rand(1,100); %y-coordinate of a point
z = H*rand(1,100); %z-coordinate of a point
blob = 10;
plot3(x, y, z,'o', 'MarkerFaceColor','b','MarkerEdgeColor','b','MarkerSize',blob);
box
This sets the size of all the points to be the same.
If you want some to be set at different values you would need to plot them with seperate plot commands.
Don jaya
Don jaya 2020 年 8 月 28 日
Thanks alot

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePhased Array Design and Analysis についてさらに検索

質問済み:

2020 年 8 月 28 日

コメント済み:

2020 年 8 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by