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:
I need help vectorizing my code please

Subject: I need help vectorizing my code please

From: Lisa

Date: 24 Jun, 2010 02:38:05

Message: 1 of 7

In my simulations these loops need to be run through almost 200000 times. With my clumsy programming I estimate that this will take almost 20 days! I would really appreciate some help in vectorising and hopefully optimising this chunck of code (below)

Thanks!

%Y, A and O are matrices
%c and r are vectors
%d is scalar

for i = 1:final
    d = SMALL_VALUE;
    for j = 1:final
         Y(i,j)=A(i,j)*r(j);
          d = d + Y(i,j);
     end
     for j = 1:final
          O(i,j) = (c(i)/d) * Y(i,j);
     end
end
for j = 1:final
     d = SMALL_VALUE;
     for i =1:final
          d = d + (A(i,j)*r(j) - O(i,j))* A(i,j);
     end
     r(j)= r(j) - d/p(j);
end

Subject: I need help vectorizing my code please

From: Roger Stafford

Date: 24 Jun, 2010 03:32:04

Message: 2 of 7

"Lisa " <lwillats@unimelb.edu.au> wrote in message <hvuged$omr$1@fred.mathworks.com>...
> In my simulations these loops need to be run through almost 200000 times. With my clumsy programming I estimate that this will take almost 20 days! I would really appreciate some help in vectorising and hopefully optimising this chunck of code (below)
>
> Thanks!
>
> %Y, A and O are matrices
> %c and r are vectors
> %d is scalar
>
> for i = 1:final
> d = SMALL_VALUE;
> for j = 1:final
> Y(i,j)=A(i,j)*r(j);
> d = d + Y(i,j);
> end
> for j = 1:final
> O(i,j) = (c(i)/d) * Y(i,j);
> end
> end
> for j = 1:final
> d = SMALL_VALUE;
> for i =1:final
> d = d + (A(i,j)*r(j) - O(i,j))* A(i,j);
> end
> r(j)= r(j) - d/p(j);
> end
- - - - - - - - - -
 Y = A.*repmat(r,final,1); % r a row vector
 O = Y.*repmat(c./(sum(Y,2)+SMALL_VALUE),1,final); % c a col. vector
 r = r - (sum((Y-O).*A,1)+SMALL_VALUE)./p; % p a row vector

This assumes that r and p are row vectors and c is a column vector.

Roger Stafford

Subject: I need help vectorizing my code please

From: Matt Fig

Date: 24 Jun, 2010 03:33:03

Message: 3 of 7

Did you pre-allocate your arrays? How large are your input arrays? If you did pre-allocate, I would be very surprised it this took 20 days to run. Your first FOR loop can be vectorized as follows (I assume c and r are row vectors):



Y2 = bsxfun(@times,A,r);
D = SMALL_VALUE + cumsum(Y2,2);
D = D(:,final);
O2 = bsxfun(@times,Y2,c.'./D);


I don't have time to do the second loop, but I still wonder about your speeds. If you had pre-allocated Y and O, your code should still be pretty fast - depending on the sizes involved.

Subject: I need help vectorizing my code please

From: Matt Fig

Date: 24 Jun, 2010 03:56:03

Message: 4 of 7

Alright, so I do have time. This is similar to Roger's solution except for the use of bsxfun. If you don't need the final values of d, this could be simplified even further.


Y2 = bsxfun(@times,A,r2);
D = SMALL_VALUE + sum(Y2,2);
O2 = bsxfun(@times,Y2,c'./D);
D = sum((bsxfun(@times,A,r2) - O).*A) + SMALL_VALUE;
r2 = r2 - D./p;

Subject: I need help vectorizing my code please

From: Lisa

Date: 24 Jun, 2010 04:40:21

Message: 5 of 7

Thanks for your time! This is really helpful. I forgot to mention in the orginal post that 'p' is a matrix, so p(j) refers to the jth column of the matrix. I assume this will affect the last line of your code (and Roger's)?

Cheers.

"Matt Fig" <spamanon@yahoo.com> wrote in message <hvul0j$i6l$1@fred.mathworks.com>...
> Alright, so I do have time. This is similar to Roger's solution except for the use of bsxfun. If you don't need the final values of d, this could be simplified even further.
>
>
> Y2 = bsxfun(@times,A,r2);
> D = SMALL_VALUE + sum(Y2,2);
> O2 = bsxfun(@times,Y2,c'./D);
> D = sum((bsxfun(@times,A,r2) - O).*A) + SMALL_VALUE;
> r2 = r2 - D./p;

Subject: I need help vectorizing my code please

From: Lisa

Date: 24 Jun, 2010 05:16:05

Message: 6 of 7

Actually, no worries, all fixed up. Thanks again!

"Lisa " <lwillats@unimelb.edu.au> wrote in message <hvunjk$3ok$1@fred.mathworks.com>...
> Thanks for your time! This is really helpful. I forgot to mention in the orginal post that 'p' is a matrix, so p(j) refers to the jth column of the matrix. I assume this will affect the last line of your code (and Roger's)?
>
> Cheers.
>
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hvul0j$i6l$1@fred.mathworks.com>...
> > Alright, so I do have time. This is similar to Roger's solution except for the use of bsxfun. If you don't need the final values of d, this could be simplified even further.
> >
> >
> > Y2 = bsxfun(@times,A,r2);
> > D = SMALL_VALUE + sum(Y2,2);
> > O2 = bsxfun(@times,Y2,c'./D);
> > D = sum((bsxfun(@times,A,r2) - O).*A) + SMALL_VALUE;
> > r2 = r2 - D./p;

Subject: I need help vectorizing my code please

From: Lisa

Date: 24 Jun, 2010 06:17:25

Message: 7 of 7

Thanks so much. Simulation time now down to less than 1 day!

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hvujjk$jg1$1@fred.mathworks.com>...
> "Lisa " <lwillats@unimelb.edu.au> wrote in message <hvuged$omr$1@fred.mathworks.com>...
> > In my simulations these loops need to be run through almost 200000 times. With my clumsy programming I estimate that this will take almost 20 days! I would really appreciate some help in vectorising and hopefully optimising this chunck of code (below)
> >
> > Thanks!
> >
> > %Y, A and O are matrices
> > %c and r are vectors
> > %d is scalar
> >
> > for i = 1:final
> > d = SMALL_VALUE;
> > for j = 1:final
> > Y(i,j)=A(i,j)*r(j);
> > d = d + Y(i,j);
> > end
> > for j = 1:final
> > O(i,j) = (c(i)/d) * Y(i,j);
> > end
> > end
> > for j = 1:final
> > d = SMALL_VALUE;
> > for i =1:final
> > d = d + (A(i,j)*r(j) - O(i,j))* A(i,j);
> > end
> > r(j)= r(j) - d/p(j);
> > end
> - - - - - - - - - -
> Y = A.*repmat(r,final,1); % r a row vector
> O = Y.*repmat(c./(sum(Y,2)+SMALL_VALUE),1,final); % c a col. vector
> r = r - (sum((Y-O).*A,1)+SMALL_VALUE)./p; % p a row vector
>
> This assumes that r and p are row vectors and c is a column vector.
>
> Roger Stafford

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