I have currently written this code to calculate the radial distance for all points with their x, y and z coordinates.
for x = xmin:step:xmax for y = ymin:step:ymax for z = zmin:step:zmax r((x-xmin)/step+1,(y-ymin)/step+1,(z-zmin)/step+1) = sqrt(x^2 + y^2 + z^2);
end end end
I am wondering if there is a way to vectorize this code so that I don't have to run the nested loops and hence speed up my calculation time, because I have to run the code for quite a number of times to analyze different data sets.
I am new to matlab so this question may seem quite trivial but please do help me :) Thank you in advance.
x = (xmin:step:xmax).'; y = ymin:step:ymax; z = reshape(zmin:step:zmax,1,1,);
r=sqrt(bsxfun(@plus, bsxfun(@plus,x.^2,y.^2),z.^2 ));