Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem vectorizing

Subject: problem vectorizing

From: Zsolt

Date: 28 Nov, 2008 23:30:23

Message: 1 of 4

I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help? Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for

X = linspace(-a/2,a/2,length(Gpp));
[X Y] = meshgrid(X);
r = sqrt((X).^2+(Y).^2);
tic
ep = zeros(size(Egp));

for m=1:length(Gpp)
    for n=1:length(Gpp)
        ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);
    end
end

Subject: problem vectorizing

From: Jan Simon

Date: 29 Nov, 2008 01:59:02

Message: 2 of 4

Dear Zsolt!

> X = linspace(-a/2,a/2,length(Gpp));
> [X Y] = meshgrid(X);
> r = sqrt((X).^2+(Y).^2);
> tic
> ep = zeros(size(Egp));
>
> for m=1:length(Gpp)
> for n=1:length(Gpp)
> ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);
> end
> end

Can you give us a definition of Gpp and Egp?
Is LENGTH(Gpp) save enough - I'd prefer SIZE(Gpp,1).

Kind regards, Jan

Subject: problem vectorizing

From: Roger Stafford

Date: 29 Nov, 2008 02:16:01

Message: 3 of 4

"Zsolt " <zlpst@yahoo.com> wrote in message <ggpuuf$scq$1@fred.mathworks.com>...
> I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help? Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for
>
> X = linspace(-a/2,a/2,length(Gpp));
> [X Y] = meshgrid(X);
> r = sqrt((X).^2+(Y).^2);
> tic
> ep = zeros(size(Egp));
>
> for m=1:length(Gpp)
> for n=1:length(Gpp)
> ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);
> end
> end

  I presume Gpp, Egp, r, and ep are all square matrices of the same size. You might conceivably gain a little time by changing the order here and completing the summation within each loop as in:

 ep = zeros(size(Egp));
 for m=1:length(Gpp)
   for n=1:length(Gpp)
     ep(m,n) = sum(sum(Egp.*exp(i*Gpp*r(m,n))));
   end
 end

(or then again this might not help.)

  However, I don't see the need for any further vectorization. If, as you indicate, these matrices are of large size, the overhead involved in the for-loop indexing should be quite small compared with the time used in the summation, multiplication, and exponentiation computations within each loop period. I remind you that, as you have written the code, you are dealing here with every possible combination of elements from the matrix r with those of Gpp and Egp, which makes it an order(length(Gpp)^4) procedure and that is bound to take a lot of time for large length(Gpp). Each doubling of length(Gpp) will multiply the time of execution by a factor of approximately sixteen! Vectorization is not going to get you around that.

Roger Stafford

Subject: problem vectorizing

From: Zsolt

Date: 29 Nov, 2008 20:01:02

Message: 4 of 4

"Zsolt " <zlpst@yahoo.com> wrote in message <ggpuuf$scq$1@fred.mathworks.com>...
> I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help? Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for
>
> X = linspace(-a/2,a/2,length(Gpp));
> [X Y] = meshgrid(X);
> r = sqrt((X).^2+(Y).^2);
> tic
> ep = zeros(size(Egp));
>
> for m=1:length(Gpp)
> for n=1:length(Gpp)
> ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);
> end
> end

Thank you guys for the responses, Gpp and Egp are square 2D matrices of hundreds of thousands of ellements. That is correct, Every value of Gpp is smeared across the r matrix and the same with Egp.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us