フィルターのクリア

Getting rid of a for-loop (in order to optimise execution time) for 2 operations that involve the inverse of a 3D matrix

1 回表示 (過去 30 日間)
I am working on a RT Simulink implementation of an interacting multi-model Kalmanfilter used for event-detection in Fusion devices.
I am trying to get rid of all for-loops in the code to optimise for execution time. I already had help with my first problem, which could be solved by making use of the reshape-function.
However, I tried various ways to use this function for another set of for-loops that I have, but It does not work due to the fact that it is hard to calculate the inverse of a 3D matrix without for-loops to my knowledge.
The problem:
Nmodel = 3
exparg = zeros(Nmodel,1);
L = zeros(Nmodel,1);
for ii=1:Nmodel
exparg(ii) = -.5*z(:,ii)'/S(:,:,ii)*z(:,ii); % Calculate arguments for the exp used in the Likelihood calculation
end
exparg = exparg - max(exparg); % Only the Likelihood ratio's matter, so normalise expargs to prevent exp(-1000) = 0
for ii=1:Nmodel
L(ii) = 1./sqrt(det(S(:,:,ii))) * exp(exparg(ii)); % Model likelihoods based on measurement correspondence
end
L = L/sum(L); % Normalise likelihoods for plotting purposes
If somebody has any suggestions on which methods I could use to get rid of the for-loops (without introducing even slower operations) in order to optimise execution, I would be very thankful.
I know that this is the second question within a day, but changing for-loops to matrix multiplications is not my expertise, given that I do not know the "inner workings" of most matlab-functions.
Thanks a lot in advance!
PS: I know of the danger of taking 1/sqrt(det) if the determinant would be zero, but I have to implement the version of my predecessor before changing the code to much.
Ricardo
EDIT: so z has size (3,3) ; S has size (3,3,3); L has size(3,1); exparg has size (3,1)
  1 件のコメント
Matt J
Matt J 2017 年 8 月 5 日
Optimizing such short loops in isolation from the rest of the code isn't going to make a significant improvement in execution time. You would need Nmodel in the hundreds or thousands until before it would even start to make a difference.
You need to address the outer loop which is calling the code segment you have shown and consider whether there is a way to vectorize that.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by