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:
Vectorization

Subject: Vectorization

From: Mariano

Date: 13 Nov, 2011 21:35:10

Message: 1 of 4

I have the following for-loop:
for n=1:N-1
   Baux = B(n,:);
   A(n+1) = A(n) + Baux*mhu + Baux*sigma*Baux';
   B(n+1,:) = Baux*theta -delta;
end

where B(1,:) and A(1) are given
Matrix B is 120x60
Matrix A is 120x1
mhu is 60x1
sigma is 60x60, theta is 60x60
delta is 1x60
N = 120

Is there any way to speed this up? Like some clever vectorization that I can't figure out?
I can actually solve for B in closed form and write it as an inline function of n but i doubt it will be faster
Any suggestions? Thank you!

Mariano

Subject: Vectorization

From: Roger Stafford

Date: 14 Nov, 2011 16:47:08

Message: 2 of 4

"Mariano" wrote in message <j9pd6e$6ov$1@newscl01ah.mathworks.com>...
> I have the following for-loop:
> for n=1:N-1
> Baux = B(n,:);
> A(n+1) = A(n) + Baux*mhu + Baux*sigma*Baux';
> B(n+1,:) = Baux*theta -delta;
> end
>
> where B(1,:) and A(1) are given
> Matrix B is 120x60
> Matrix A is 120x1
> mhu is 60x1
> sigma is 60x60, theta is 60x60
> delta is 1x60
> N = 120
>
> Is there any way to speed this up? Like some clever vectorization that I can't figure out?
> I can actually solve for B in closed form and write it as an inline function of n but i doubt it will be faster
> Any suggestions? Thank you!
>
> Mariano
- - - - - - - -
  I assume your "closed form" is

 B(n,:) = (B(1,:)-delta/(theta-eye(60)))*V*D^(n-1)*V' ...
                + delta/(theta-eye(60)) ;

where theta = V*D*V' is the eigenvector/eigenvalue decomposition of theta. This might be an effective shortcut for evaluating just B(120,:) alone, but if you need all the B(n,:) values, I suspect that your for-loop is about as efficient a method as you can find. To apply the above closed form to each value of n would obviously be less efficient.

Roger Stafford

Subject: Vectorization

From: Roger Stafford

Date: 14 Nov, 2011 16:58:10

Message: 3 of 4

"Roger Stafford" wrote in message <j9rgmc$237$1@newscl01ah.mathworks.com>...
> B(n,:) = (B(1,:)-delta/(theta-eye(60)))*V*D^(n-1)*V' ...
> + delta/(theta-eye(60)) ;
- - - - - - - - - -
Note: I should have written the more efficient diag(D).^(n-1) instead of D^(n-1).

Roger Stafford

Subject: Vectorization

From: Mariano

Date: 14 Nov, 2011 19:41:14

Message: 4 of 4

Thanks, Roger. Yes, unfortunately I'm interested in all the rows of B. If i had to run this once, I wouldn't even bother, but the loop is called millions of times by an optimization function. Anyway, I appreciate your help.
Best,

Mariano


"Roger Stafford" wrote in message <j9rgmc$237$1@newscl01ah.mathworks.com>...
> "Mariano" wrote in message <j9pd6e$6ov$1@newscl01ah.mathworks.com>...
> > I have the following for-loop:
> > for n=1:N-1
> > Baux = B(n,:);
> > A(n+1) = A(n) + Baux*mhu + Baux*sigma*Baux';
> > B(n+1,:) = Baux*theta -delta;
> > end
> >
> > where B(1,:) and A(1) are given
> > Matrix B is 120x60
> > Matrix A is 120x1
> > mhu is 60x1
> > sigma is 60x60, theta is 60x60
> > delta is 1x60
> > N = 120
> >
> > Is there any way to speed this up? Like some clever vectorization that I can't figure out?
> > I can actually solve for B in closed form and write it as an inline function of n but i doubt it will be faster
> > Any suggestions? Thank you!
> >
> > Mariano
> - - - - - - - -
> I assume your "closed form" is
>
> B(n,:) = (B(1,:)-delta/(theta-eye(60)))*V*D^(n-1)*V' ...
> + delta/(theta-eye(60)) ;
>
> where theta = V*D*V' is the eigenvector/eigenvalue decomposition of theta. This might be an effective shortcut for evaluating just B(120,:) alone, but if you need all the B(n,:) values, I suspect that your for-loop is about as efficient a method as you can find. To apply the above closed form to each value of n would obviously be less efficient.
>
> Roger Stafford

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