Replacing a for loop with matrix multiplication
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
James Tursa
2021년 2월 1일
편집: James Tursa
2021년 2월 1일
result = sum(x.*(inv(SIG)*x));
or
result = sum(x.*(SIG\x));
댓글 수: 1
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!