The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: meshgrid alternative?
Replies: 5   Last Post: Sep 26, 2003 1:27 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Stefan Stoll

Posts: 312
Registered: 12/7/04
Re: meshgrid alternative?
Posted: Sep 26, 2003 10:19 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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;

%ans =
% 0.3600

t = cputime;

%ans =
% 0.2500

[x2,y2,z2] = meshgrid(x.^2,y.^2,z.^2);
rho = sqrt(x2+y2+z2);

%ans =
% 0.1900

Maybe this is a reason to upgrade to Matlab 6.x.


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.