Stephen wants to speed up > > > [l,k,j]=meshgrid(x,y,z); > > > > > > rho=(l.^2+k.^2+j.^2).^(0.5);
sTefan's suggestion: > > N = 200; > > x = rand(1,N); y = rand(1,N); z = rand(1,N); > > > > [x2,y2,z2] = meshgrid(x.^2,y.^2,z.^2); > > rho = sqrt(x2+y2+z2);
Stephen McGovern answered: > The function SQRT cut the time by roughly %5.5. Squaring before hand cut > another > %1.5 (version 5.1), but it does make my code slightly more confusing....
No idea how to improve your code for old Matlab 5.1. Maybe the meshgrid function in 5.1 can be streamlined a bit for your case, but don't expect large speed gains.
I am a bit wondering why squaring beforehand gave only 1.1% performance increase. Theoretically, only 3*N instead of N^3 .^2 have to be computed, with a corresponding reduction in computation time by a factor of approx. N^2/3. What is N in your case?
In Matlab 6.5 I get a significant perfomance increase even for quite small N:
N = 100; x = rand(1,N); y = rand(1,N); z = rand(1,N);
t = cputime; [l,k,j]=meshgrid(x,y,z); rho=(l.^2+k.^2+j.^2).^(0.5); cputime-t
%ans = % 0.3600
t = cputime; [l,k,j]=meshgrid(x,y,z); rho=sqrt(l.^2+k.^2+j.^2); cputime-t