In the problem I have here, x is a 2x300 matrix and SIG is a 2x2 matrix.
What I'm looking for is a 1x300 vector, where the ith entry corresponds to x(:,i)'*inv(SIG)*x(:,i). I've written this out in a for loop below:
ans = zeros(1,size(x,2));
for i = 1:size(x,2)
ans(i) = x(:,i)'*inv(SIG)*x(:,i);
end
but I think there must be a way to make this more efficient with some sort of matrix multiplication--I just can't figure it out.
Any help appreciated!

1 件のコメント

Stephen23
Stephen23 2021 年 2 月 1 日
編集済み: Stephen23 2021 年 2 月 1 日
Note that using inv is less efficient than using mldivide:

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2021 年 2 月 1 日
編集済み: James Tursa 2021 年 2 月 1 日

0 投票

result = sum(x.*(inv(SIG)*x));
or
result = sum(x.*(SIG\x));

1 件のコメント

Bruno Luong
Bruno Luong 2021 年 2 月 1 日
In case complex data
result = sum(conj(x).*(SIG\x));

サインインしてコメントする。

その他の回答 (1 件)

J Chen
J Chen 2021 年 2 月 1 日

0 投票

Try x'*inv(SIG)*x. Matlab can directly handle vectors and matrices.

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by