help-octave
[Top][All Lists]

Re: Summation and Mesh Plot

 From: Jordi Gutiérrez Hermoso Subject: Re: Summation and Mesh Plot Date: Wed, 4 Aug 2010 09:18:15 -0500

On 29 July 2010 07:08, Anan <address@hidden> wrote:
>
> I am trying to get the mesh plot which involve a summation.
> the code is
>
> x=[-4:0.1:4];
> y=x;
> [xx,yy]=meshgrid(x,y);
> fac1=0.0+0.0j;
> fac2=0.0+0.0j;
> for a=1:10
> for c=1:10
>     fac2=fac2+exp(j*(c-a).*xx);
> endfor
> endfor
> for b=1:10
> for a=1:10
>     fac1=fac1+exp(j*(a-b).*yy)
> endfor
> endfor
> mesh(xx,yy,abs(fac1.*fac2).^2);

This isn't terrible. You have a few for loops, but they're not slowing
you down noticeably. Here's one way to vectorise this. The trick is
basically that you can delay a few meshgrids and you can use kron to
generate a matrix across which you can sum.

x=[-4:0.1:4];
y=x;

[xx,yy]=meshgrid(x,y);

fac1 = 0;
fac2 = 0;

a = [1:10];

[aa,bb] = meshgrid(a,a);

cc = (bb-aa)(:);

fac1 = sum(exp(j*kron(cc,x)));

[fac2, fac1] = meshgrid(fac1,fac1);

mesh(xx,yy,abs(fac1.*fac2).^2)

HTH,
- Jordi G. H.

reply via email to