Thread Subject: Vectorizing Double For Loops

Subject: Vectorizing Double For Loops

From: omegayen

Date: 31 Jul, 2009 05:00:20

Message: 1 of 9

So I am having trouble vectorizing double for loops so they are more efficient...

For example...

for yy=1:n
    for uu=1:n
        X(yy,uu)=Y(yy,uu)*W(yy,1)*Constant;
    end
end

the problem is multiplying by matrix dimensions that are not the same. thanks for your help

Subject: Vectorizing Double For Loops

From: Matt Fig

Date: 31 Jul, 2009 06:02:01

Message: 2 of 9

Your double For loop can be written as:

X = bsxfun(@times,Y,W(:,1))*Constant

and for large arrays will certainly be faster than a double For loop.

Subject: Vectorizing Double For Loops

From: Bruno Luong

Date: 31 Jul, 2009 06:07:01

Message: 3 of 9

"omegayen " <omegayen@ameritech.net> wrote in message <h4ttp3$acv$1@fred.mathworks.com>...
> So I am having trouble vectorizing double for loops so they are more efficient...
>
> For example...
>
> for yy=1:n
> for uu=1:n
> X(yy,uu)=Y(yy,uu)*W(yy,1)*Constant;
> end
> end
>

X = Y .* repmat(W(:),1,size(Y,2)) * Constant

% OR

X = Constant * bsxfun(@times,Y,W(:))

% OR

X = Constant * (diag(W(:)) * Y)

% OR

X = diag(Constant * W(:) )* Y

% Bruno

Subject: Vectorizing Double For Loops

From: omegayen

Date: 1 Aug, 2009 18:17:02

Message: 4 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h4u1m5$dm2$1@fred.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <h4ttp3$acv$1@fred.mathworks.com>...
> > So I am having trouble vectorizing double for loops so they are more efficient...
> >
> > For example...
> >
> > for yy=1:n
> > for uu=1:n
> > X(yy,uu)=Y(yy,uu)*W(yy,1)*Constant;
> > end
> > end
> >
>
> X = Y .* repmat(W(:),1,size(Y,2)) * Constant
>
> % OR
>
> X = Constant * bsxfun(@times,Y,W(:))
>
> % OR
>
> X = Constant * (diag(W(:)) * Y)
>
> % OR
>
> X = diag(Constant * W(:) )* Y
>
> % Bruno

thanks Bruno that worked great..... perhaps you can help me with another, I can't figure out how to do it for some reason...

I want to compute the distance between points..... using 2 column vectors and ending with a square distance matrix

essentially

x= (column vector length n)
y= (column vector length n)

for i=1:n
    for j=1:n
        distance = sqrt ((x(i) - x(j)).^2 + (y(i) - y(j)).^2)
    end
end

the above implementation works, but I am trying to speed it up somehow, any thoughts would be welcomed

Subject: Vectorizing Double For Loops

From: Bruno Luong

Date: 1 Aug, 2009 18:48:03

Message: 5 of 9

"omegayen " <omegayen@ameritech.net> wrote in message <h520qu$eo4$1@fred.mathworks.com>...

>
> essentially
>
> x= (column vector length n)
> y= (column vector length n)
>
> for i=1:n
> for j=1:n
> distance = sqrt ((x(i) - x(j)).^2 + (y(i) - y(j)).^2)
> end
> end
>
> the above implementation works, but I am trying to speed it up somehow, any thoughts would be welcomed

This should do:

dx = bsxfun(@minus,x(:),x(:).');
dy = bsxfun(@minus,y(:),y(:).');
distance = sqrt(dx.^2+dy.^2)

% Bruno

Subject: Vectorizing Double For Loops

From: omegayen

Date: 1 Aug, 2009 19:05:06

Message: 6 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h522l2$745$1@fred.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <h520qu$eo4$1@fred.mathworks.com>...
>
> >
> > essentially
> >
> > x= (column vector length n)
> > y= (column vector length n)
> >
> > for i=1:n
> > for j=1:n
> > distance = sqrt ((x(i) - x(j)).^2 + (y(i) - y(j)).^2)
> > end
> > end
> >
> > the above implementation works, but I am trying to speed it up somehow, any thoughts would be welcomed
>
> This should do:
>
> dx = bsxfun(@minus,x(:),x(:).');
> dy = bsxfun(@minus,y(:),y(:).');
> distance = sqrt(dx.^2+dy.^2)
>
> % Bruno

I forgot to specify that I can't use bsxfun with class sym (I am trying to use class sym in the implementation) so the method works great but bsxfun doesnt work with class sym

Subject: Vectorizing Double For Loops

From: Matt Fig

Date: 1 Aug, 2009 19:18:03

Message: 7 of 9

This is faster than the bsxfun soln anyway for n = 300.

distance = zeros(n,n); % Pre-allocate, Pre-allocate, Pre-allocate!
for ii=1:n
    for jj=ii:n % No need to start at 1 and duplicate calculations.
        distance(ii,jj) = sqrt((x(ii) - x(jj)).^2 + (y(ii) - y(jj)).^2);
    end
end
distance = distance + distance.'; % Do you really need the duplicate data (this step)?

Subject: Vectorizing Double For Loops

From: omegayen

Date: 2 Aug, 2009 04:40:18

Message: 8 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <h524db$pp3$1@fred.mathworks.com>...
> This is faster than the bsxfun soln anyway for n = 300.
>
> distance = zeros(n,n); % Pre-allocate, Pre-allocate, Pre-allocate!
> for ii=1:n
> for jj=ii:n % No need to start at 1 and duplicate calculations.
> distance(ii,jj) = sqrt((x(ii) - x(jj)).^2 + (y(ii) - y(jj)).^2);
> end
> end
> distance = distance + distance.'; % Do you really need the duplicate data (this step)?

That should speed it up Matt, thanks....

does anyone know any implementation of Bruno's suggestion for class sym.....

basically can you multiply a column vector of length n by another column vector of length n to create a square matrix n x n using class sym?

Subject: Vectorizing Double For Loops

From: omegayen

Date: 13 Aug, 2009 21:15:26

Message: 9 of 9

"omegayen " <omegayen@ameritech.net> wrote in message <h535bi$2hl$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <h524db$pp3$1@fred.mathworks.com>...
> > This is faster than the bsxfun soln anyway for n = 300.
> >
> > distance = zeros(n,n); % Pre-allocate, Pre-allocate, Pre-allocate!
> > for ii=1:n
> > for jj=ii:n % No need to start at 1 and duplicate calculations.
> > distance(ii,jj) = sqrt((x(ii) - x(jj)).^2 + (y(ii) - y(jj)).^2);
> > end
> > end
> > distance = distance + distance.'; % Do you really need the duplicate data (this step)?
>
> That should speed it up Matt, thanks....
>
> does anyone know any implementation of Bruno's suggestion for class sym.....
>
> basically can you multiply a column vector of length n by another column vector of length n to create a square matrix n x n using class sym?

still wondering if anyone knows how to implement this without double for loops and for class sym, thanks

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
reference Sprinceana 31 Jul, 2009 02:07:55
bsxfun Sprinceana 31 Jul, 2009 02:07:55
vectorize for l... Sprinceana 31 Jul, 2009 02:07:36
rssFeed for this Thread

Contact us at files@mathworks.com