0

I'm using the profiler on my MATLAB code, and the following line is taking 50% of the code's computation time:

firstSide = -1*HessianInverse*(eye(n,n)- ...
currentA'(currentA*HessianInverse*currentA')^-1*currentA*HessianInverse)*currentG;
  • HessianInverse is an n x n matrix
  • currentG is an n x n matrix
  • currentA is an n x n matrix as well

What would be the fastest way of doing this computation?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114

1 Answers1

2

You can do two simple things:

  • Compute currentA*HessianInverse once and store the result, since your are using the same matrix multiplication in two different places.
  • Replace ^-1 with the \ operator, since the latter is about twice as fast:

For example

>> A = rand(1000);
>> B = rand(1000);

>> tic; A^-1*B; toc
Elapsed time is 0.592531 seconds.
>> tic; A^-1*B; toc
Elapsed time is 0.578318 seconds.

>> tic; A\B; toc
Elapsed time is 0.275542 seconds.
>> tic; A\B; toc
Elapsed time is 0.262008 seconds.
Kavka
  • 4,191
  • 16
  • 33