I am cycling through some geometry in 3D space and writing images that are essentially slices from the volume. The image generation routine is shown below. As you can see, it has several for loops and is quite slow. Any suggestions on speeding this thing up?
for oo = 1:Height I = zeros(Depth,Width,'int8'); for jj = 1:r rad = M(jj,4); if abs(oo-(Height-M(jj,3))) < rad front = ceil(Depth - M(jj,1) + rad); back = floor(Depth - M(jj,1) - rad)+1; left = floor(M(jj,2) - rad)+1; right = ceil(M(jj,2) + rad); for nn = back:front for kk = left:right if ((kk - M(jj,2))^2+(nn-(Depth-M(jj,1)))^2+ ... (oo-(Height-M(jj,3)))^2)<=rad^2 I(nn,kk) = 1; end end end end end imwrite(logical(I),['data' num2str(oo,'%04d') 'r.tif']); ['File number ',num2str(oo,'%04d'),' has been processed'] end