Hi, generatin random points inside a cuboid problem

조회 수: 4 (최근 30일)
fatima-zahra achbah
fatima-zahra achbah 2017년 10월 9일
댓글: fatima-zahra achbah 2017년 10월 9일
Hi, I need to generate a total I of points inside a specific cuboid(a*b*c) and plot it then after havings these points and their coordinations generate a sphere for each point.I already did a script but unfortunately, it gives me a cube results :
clc
close all
a=10; % mm
b=10; % mm
depth=0.3; %mm
i= 1145916; %total number of pores and thus of points inside the parallelepiped
v=i*[];
Vt=a*b*depth; %volume expression mm^3
s = 10;
n = bsxfun(@plus,0,s.*rand(i,3));
r=normrnd(0.005,0.001,[1 i]); %mu=0,005 mm ==>5µm
x=n(:,1);
y=n(:,2);
z=n(:,3);
scatter3(x,y,z,r,'fill')
for k=1:i
v(k)=(4*pi*(r(k)^3))/3;
end
figure
plot(v)
Best regards
  댓글 수: 2
KSSV
KSSV 2017년 10월 9일
You want random points inside sphere?
fatima-zahra achbah
fatima-zahra achbah 2017년 10월 9일
no, I want random points inside my cuboid and then draw a sphere on each point

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 9일
n = bsxfun(@plus,0,s.*rand(i,3));
The bsxfun to add 0 is not doing anything useful there. You might as well have
n = s .* rand(i,3);
However, the reason you get a cube is that you are not weighting the coordinates according to the size of their dimension. I would suggest,
n = rand(i, 3) * diag([a, b, depth]); %* rather than .* is deliberate
Or if you are using R2016b or later, you could use
n = rand(i, 3) .* [a, b, depth] ; %needs R2016b or later.
The bsxfun equivalent would be
n = bsxfun(@times, [a, b, depth], rand(i,3) );
In my R2017b system, the time required for bsxfun(@times, data, s) and data .* s are pretty much indistinguishable, and the time for data * diag(s) is about 50% more.
  댓글 수: 1
fatima-zahra achbah
fatima-zahra achbah 2017년 10월 9일
Oh my god, it works, thank you so much, I truly appreciate your help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by