Matrix points inside of a sphere
Show older comments
How to check if points of matrix are inside of a sphere, which is given by its radius and centre?
1 Comment
Jon
on 23 Mar 2021
Please clarify what are the dimension of your matrix and what data is in the elements of this matrix
Answers (2)
Star Strider
on 23 Mar 2021
Edited: Star Strider
on 23 Mar 2021
Try this:
[X,Y,Z] = sphere(20);
ctr = mean([X(:),Y(:),Z(:)]);
r = max(Z(:)) - ctr(3);
xp = 1-2*rand(150,1);
yp = 1-2*rand(150,1);
zp = 1-2*rand(150,1);
insph = (xp.^2 + yp.^2 + zp.^2) <= r^2;
figure
mesh(X,Y,Z,'FaceAlpha',0.5)
hold on
plot3(xp(insph), yp(insph), zp(insph), '.r')
plot3(xp(~insph), yp(~insph), zp(~insph), '.b')
hold off
axis('equal')
legend('Sphere','Inside Sphere','Outside Sphere')
Experiment with this approach to get results appropriate to your data.
EDIT — (23 Mar 2021 at 13:12)
Added plot figure —

.
4 Comments
K_A
on 24 Mar 2021
Follow up question,
How can we stop particles to enter into the sphere if the particle motion is doing random walk meaning x(current) = x(previous) + gaussian noise
y(current) = y(previous) + gaussian noise
z(current) = z(previous) + gaussian noise
Thanks in advance
Arvind
Star Strider
on 24 Mar 2021
I would do something similar to calculating ‘insph’ for each particle to determine its location relative to the sphere.
If it is on the surface of the sphere, specifically if:
insph_fcn = @(xp,yp,xp) (xp.^2 + yp.^2 + zp.^2) - r^2;
equals 0, (or is slilghtly different than 0 if the increment ‘overshoots’ the sphere boundary, such that the differenced is less than or equal to the increment that it moved) then stop it or reverse its direction or whatever it is supposed to do in that event. Either call the ‘insph_fcn’ once for each particle individually, or do something similar in a loop for all particles at once, and then have them do whatever you want them to do if any of them are on the surface.
That is how I would do it. This is hypothetical, since I have not actually coded that beyond what is in my original Answer.
K_A
on 24 Mar 2021
Thanks Star Strider for your kind response. I am using random walk approch with sphere in the domain. I am
assuming sphere as elastic boundary, that is if x(current) lies across a sphere boundary, then its reflected (r) position is
x(r-current) = 2(x(previous) + projection onto the sphere boundary (x(current) - x(previous)) - x(current). This way particle will reflect back from the boundary.
Any suggestion or help would be appreciated.
Star Strider
on 24 Mar 2021
In that event, simply reverse (likely change the sign of) one or more of the directions of the particle from the previous directions when the value returned by ‘insph_fcn’ is within some tolerance of 0, in the subsequent time interval.
I leave those details to you.
If the Euclidean distance from the point to the sphere center is less than the radius, then it's inside. Something like this:
% define the sphere
radius=10;
center=[100 100 100];
% pick a point
point=[1 1 1]*100;
% for this example "inside" includes "on the surface"
% you'll have to decide what that case means in your context
% this returns a logical scalar
inside=(radius^2>=sum((point-center).^2))
... and so on. Note that we don't need to take the square root of anything if we square the radius. Assuming radius is a constant, we can precalculate the square and avoid needing to make the extra calculation if (for instance) the distance is being tested many times in a loop.
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!