MATLAB: How to place a single colored 3d solid sphere in a 512x512x512 image

3d sphere

Hello,
How can I place a single colored 3d solid sphere in a 512x512x512 image? im = zeros(512,512,512,'double');
The sphere has a center: x1=200 y1=200 z1=200 and r=25
I tried using [X, Y, Z] = sphere(n); No luck
Regards

Best Answer

% im = zeros(512, 512, 512, 'double');
x1 = 200;
y1 = 200;
z1 = 200;
r = 25;
v = 1:512;
vx = (v - x1) .^ 2;
vy = (v - y1) .^ 2;
vz = (v - z1) .^ 2;
im = bsxfun(@plus, bsxfun(@plus, vx.', vy), reshape(vz, [1, 1, 512])) < r^2;
Or in modern Matlab versions the last line is:
im = (vx.' + vy + reshape(vz, [1, 1, 512])) < r^2;