inv(A) versus /A. Which of the two is the correct one

1 Ansicht (letzte 30 Tage)
ektor
ektor am 12 Feb. 2015
Kommentiert: Stephen23 am 13 Feb. 2015
Hi all,
Let B be a n by 2 matrix and A is a 2 by 2 positive definite matrix. Which of the following two is correct
for i=1:n
B(i,:)*inv(A)*B(i,:)';
end
or
for i=1:n
(B(i,:)/A)*B(i,:)';
end
Best, Stef
  1 Kommentar
Stephen23
Stephen23 am 13 Feb. 2015
Don't use a loop, learn to vectorize your code. Matt J's answer shows this.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 12 Feb. 2015
Both are equivalent. For large matrices the 2nd would be more efficient.
Further efficiency would be gained by vectorizing. The whole loop is just,
sum( (B/A).*B, 2)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by