Most efficient way to do matrix operation v'*M*v

1 view (last 30 days)
Glen on 18 Sep 2014
Answered: Roger Stafford on 18 Sep 2014
Hi all,
i have a problem where I need to do the following operation:
R is a square matrix
V is a nonsquare matrix
The operation is to multiply 1 - V(i, :)*inv( R )*V(i, :)', and store the result for each i.
Right now I'm doing it using a for loop:
Rinv = inv( R );
for i=1:n
val(i) = 1 - Z(i, :)*Rinv*Z(i, :)';
My problem requires performing this calculation a few million times and I'm trying to optimize it as much as possible. Is there a way to get rid of the for loop? I could do V*inv( R )*V', but that performs a lot more inner products than I actually need.
Thanks for the help.

Accepted Answer

Roger Stafford
Roger Stafford on 18 Sep 2014
Assuming the values in V are real,
val = 1-sum((V/R).*V,2);
If V has complex-valued elements, change that to
val = 1-sum((V/R).*conj(V),2);
Note that V must have the same number of columns as R has rows and columns.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!