MATLAB: How to set the volume of a sphere as a logical threshold

logical arraysphereunit circlevolume

Hello, I would like to set a logical threshold as any point in 3d space within the volume of a unit sphere around a single point:
i.e xyz = [0,0,0]
inRange = xyz > volume of sphere with radius of r
Any help would be greatly appreciated.

Best Answer

sphere_radius = 17.2;
outer_limit = 30;
volume_of_sphere = 4/3 * pi * sphere_radius.^3;
center = [50, 40, 30];
coords = linspace(-outer_limit, outer_limit, 200);
[X, Y, Z] = ndgrid(coords + center(1), coords + center(2), coords + center(3));
in_sphere = (X - center(1)).^2 + (Y - center(2)).^2 + (Z - center(3)).^2 <= sphere_radius.^2;
strange_mask = X .* Y .* Z >= volume_of_sphere;
nnz(strange_mask)
strange_but_in_sphere = in_sphere & strange_mask;
nnz(strange_but_in_sphere)
If you use a center of (0, 0, 0) then no points will be both in the strange mask and in the sphere. You can prove that relatively easily for a sphere centered around the origin, the maximum x*y*z is r^3/(3*sqrt(3)) which is about 0.19 * r^3 which is always going to be less than 4/3*pi which is about 4.19. Therefore in order for a point to be such that x*y*z >= 4/3*pi*r^3, the sphere cannot be centered on the origin.
Related Question