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:
Acceleration of for loop

Subject: Acceleration of for loop

From: Johannes Korsawe

Date: 18 Feb, 2008 07:16:04

Message: 1 of 3

Dear community,

Let be size(I)=[1 201], size(poly)=[1,n+1], size(X)=[3,n+1].

With this, i have the following loop:

line=0*[I;I;I];
for i=0:n,
   line=line+[X(1,i+1)*poly{i+1};X(2,i+1)*poly{i+1};X(3,i+1)
*poly{i+1}];
end

How can i accelerate this loop? Is there any possibility of
vectorizing this statement?

Best regards,
Johannes

Subject: Acceleration of for loop

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Feb, 2008 11:50:41

Message: 2 of 3

In article <fpbbbk$q97$1@fred.mathworks.com>,
Johannes Korsawe <johannes.korsawe.nospam@volkswagen.de> wrote:

>Let be size(I)=[1 201], size(poly)=[1,n+1], size(X)=[3,n+1].

>With this, i have the following loop:

>line=0*[I;I;I];
>for i=0:n,
> line=line+[X(1,i+1)*poly{i+1};X(2,i+1)*poly{i+1};X(3,i+1)
>*poly{i+1}];
>end

>How can i accelerate this loop? Is there any possibility of
>vectorizing this statement?

Could you clarify whether poly is a cell array with n+1 elements,
each of which is a vector of 201 numbers? And that X is an
ordinary numeric array, so X(k,i) * poly{i+1} is
a scalar-vector multiplication, not a matrix multiplication?

If so, then I think replacing the loop body with
  line = line + bsxfun(@times, poly{i+1}, X(:,i+1))
might work -- but might not be faster.

To improve... let's see... cell2mat(poly) would be (n+1) x 201,
repmat that to 3 x (n+1) x 201, repmat X to 3 x (n+1) x 201,
use .* to multiply the two, sum along the second dimension, then
if need be, ipermute the 3 x 1 x 201 to 3 x 201 x 1. Or do the
repmat's slightly differently so you end up summing along the
last dimension so you automatically get 3 x 201 out of it.
--
  "When a scientist is ahead of his times, it is often through
   misunderstanding of current, rather than intuition of future truth.
   In science there is never any error so gross that it won't one day,
   from some perspective, appear prophetic." -- Jean Rostand

Subject: Acceleration of for loop

From: Johannes Korsawe

Date: 6 Mar, 2008 10:05:05

Message: 3 of 3

Hi,

you are right, inside the loop and inside the brackets, a
multiplication of a matrix with a scalar takes place. You
can find this also from the definitions in the first line
of my post.

I will check if bsxfun will help. With all this repmat, i
suspect that this will be too time consuming to reserve all
that memory.

Thank you for your reply.

Best regards,
Johannes


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fpbreh$5pm$1@canopus.cc.umanitoba.ca>...
> In article <fpbbbk$q97$1@fred.mathworks.com>,
> Johannes Korsawe <johannes.korsawe.nospam@volkswagen.de>
wrote:
>
> >Let be size(I)=[1 201], size(poly)=[1,n+1], size(X)=
[3,n+1].
>
> >With this, i have the following loop:
>
> >line=0*[I;I;I];
> >for i=0:n,
> > line=line+[X(1,i+1)*poly{i+1};X(2,i+1)*poly{i+1};X
(3,i+1)
> >*poly{i+1}];
> >end
>
> >How can i accelerate this loop? Is there any possibility
of
> >vectorizing this statement?
>
> Could you clarify whether poly is a cell array with n+1
elements,
> each of which is a vector of 201 numbers? And that X is an
> ordinary numeric array, so X(k,i) * poly{i+1} is
> a scalar-vector multiplication, not a matrix
multiplication?
>
> If so, then I think replacing the loop body with
> line = line + bsxfun(@times, poly{i+1}, X(:,i+1))
> might work -- but might not be faster.
>
> To improve... let's see... cell2mat(poly) would be (n+1)
x 201,
> repmat that to 3 x (n+1) x 201, repmat X to 3 x (n+1) x
201,
> use .* to multiply the two, sum along the second
dimension, then
> if need be, ipermute the 3 x 1 x 201 to 3 x 201 x 1. Or
do the
> repmat's slightly differently so you end up summing along
the
> last dimension so you automatically get 3 x 201 out of it.
> --
> "When a scientist is ahead of his times, it is often
through
> misunderstanding of current, rather than intuition of
future truth.
> In science there is never any error so gross that it
won't one day,
> from some perspective, appear prophetic." -- Jean
Rostand

Tags for 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