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:
bottleneck calculation

Subject: bottleneck calculation

From: Dave Brackett

Date: 2 Apr, 2008 14:38:02

Message: 1 of 7

Hi, I have a calculation which i'm trying to speed up with
no great success. It is quite a bottleneck for an
optimisation run.

Basically I have lots of 2x2 matrices which all need
multiplying and this is the bottleneck in my code. This is
currently achieved by:

for g=1:length(r0i)
prodH=prodH*[ai(g),bi(g);ci(g),di(g)];
end

Any improvement in this would significantly speed up the
calculation; is there a better way of doing this? Feel free
to ask me questions if i've not been clear. Thanks for your
help.

Subject: bottleneck calculation

From: Peter Boettcher

Date: 2 Apr, 2008 14:44:09

Message: 2 of 7

"Dave Brackett" <davebrackett@hotmail.com> writes:

> Hi, I have a calculation which i'm trying to speed up with
> no great success. It is quite a bottleneck for an
> optimisation run.
>
> Basically I have lots of 2x2 matrices which all need
> multiplying and this is the bottleneck in my code. This is
> currently achieved by:
>
> for g=1:length(r0i)
> prodH=prodH*[ai(g),bi(g);ci(g),di(g)];
> end
>
> Any improvement in this would significantly speed up the
> calculation; is there a better way of doing this? Feel free
> to ask me questions if i've not been clear. Thanks for your
> help.

See the mex file NDFUN at http://www.mit.edu/~pwb/matlab/

The 'mprod' subfunction will do this, if you assemble your ai/bi/ci/di
into a 3D matrix, one 2x2 matrix per "page".

-Peter

Subject: bottleneck calculation

From: Dave Brackett

Date: 2 Apr, 2008 14:57:02

Message: 3 of 7

Hi, thanks for your reply. Could you elaborate a bit more
on how I can do this? I am a relative novice with Matlab
and do not really understand what you mean. I have had a
look at the ndfun file but cannot find the mprod
subfunction you refer to. thanks a lot.

Subject: bottleneck calculation

From: Peter Boettcher

Date: 2 Apr, 2008 15:08:05

Message: 4 of 7

"Dave Brackett" <davebrackett@hotmail.com> writes:

> Hi, thanks for your reply. Could you elaborate a bit more
> on how I can do this? I am a relative novice with Matlab
> and do not really understand what you mean. I have had a
> look at the ndfun file but cannot find the mprod
> subfunction you refer to. thanks a lot.

help ndfun:

  'mprod' behaves differently. It cumulatively multiplies a set
    of matrices and produces a single 2D output. The equivalent
    code is:
        C = A(:,:,1);
        for i=2:N
            C = C * A(:,:,i);
        end
    2D inputs return themselves. Inputs with more than 3
    dimensions collapse the 3rd dimension only. So with an A of
    size [2 2 7 3 4],
        C = ndfun('mprod', A);
    is equivalent to
        for i=1:3
            for j=1:4
                C(:,:,i,j)=ndfun('mprod',M(:,:,:,i,j));
            end
        end
    and C will have size [2 2 3 4].


-Peter

Subject: bottleneck calculation

From: Dave Brackett

Date: 2 Apr, 2008 15:19:03

Message: 5 of 7

sorry i'm still not getting it! :S

do you think you could show how i can change my code using
the method you suggest please?
thanks for your patience, and sorry for the double post!

Subject: bottleneck calculation

From: Peter Boettcher

Date: 2 Apr, 2008 17:15:39

Message: 6 of 7


Please include quotes for context.

Dave first wrote:

> > > Hi, I have a calculation which i'm trying to speed up with
> > > no great success. It is quite a bottleneck for an
> > > optimisation run.
> > >
> > > Basically I have lots of 2x2 matrices which all need
> > > multiplying and this is the bottleneck in my code. This is
> > > currently achieved by:
> > >
> > > for g=1:length(r0i)
> > > prodH=prodH*[ai(g),bi(g);ci(g),di(g)];
> > > end
> > >
> > > Any improvement in this would significantly speed up the
> > > calculation; is there a better way of doing this? Feel free
> > > to ask me questions if i've not been clear. Thanks for your
> > > help.

To which I responded:

> > See the mex file NDFUN at http://www.mit.edu/~pwb/matlab/
> >
> > The 'mprod' subfunction will do this, if you assemble your ai/bi/ci/di
> > into a 3D matrix, one 2x2 matrix per "page".


"Dave Brackett" <davebrackett@hotmail.com> writes:

> sorry i'm still not getting it! :S
>
> do you think you could show how i can change my code using
> the method you suggest please?
> thanks for your patience, and sorry for the double post!

Assume ai/bi/ci/di are all column vectors:

M = reshape([ai bi ci di].', 2, 2, []);

% Now M(:,:,1) is the first 2x2 matrix, M(:,:,2) is the second, etc.

prodH = ndfun('mprod', M);


-Peter

Subject: bottleneck calculation

From: Dave Brackett

Date: 3 Apr, 2008 10:15:04

Message: 7 of 7

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyod8sgouc.fsf@G99-Boettcher.llan.ll.mit.edu>...
>
> Please include quotes for context.
>
> Dave first wrote:
>
> > > > Hi, I have a calculation which i'm trying to speed
up with
> > > > no great success. It is quite a bottleneck for an
> > > > optimisation run.
> > > >
> > > > Basically I have lots of 2x2 matrices which all
need
> > > > multiplying and this is the bottleneck in my code.
This is
> > > > currently achieved by:
> > > >
> > > > for g=1:length(r0i)
> > > > prodH=prodH*[ai(g),bi(g);ci(g),di(g)];
> > > > end
> > > >
> > > > Any improvement in this would significantly speed
up the
> > > > calculation; is there a better way of doing this?
Feel free
> > > > to ask me questions if i've not been clear. Thanks
for your
> > > > help.
>
> To which I responded:
>
> > > See the mex file NDFUN at
http://www.mit.edu/~pwb/matlab/
> > >
> > > The 'mprod' subfunction will do this, if you assemble
your ai/bi/ci/di
> > > into a 3D matrix, one 2x2 matrix per "page".
>
>
> "Dave Brackett" <davebrackett@hotmail.com> writes:
>
> > sorry i'm still not getting it! :S
> >
> > do you think you could show how i can change my code
using
> > the method you suggest please?
> > thanks for your patience, and sorry for the double post!
>
> Assume ai/bi/ci/di are all column vectors:
>
> M = reshape([ai bi ci di].', 2, 2, []);
>
> % Now M(:,:,1) is the first 2x2 matrix, M(:,:,2) is the
second, etc.
>
> prodH = ndfun('mprod', M);
>
>
> -Peter


ah ok, i get it now thanks for bearing with me! Cheers.

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