How can I get rid of the 2 nested for loops for my calculations which include a 3D matrix and a 2D matrix.

조회 수: 3 (최근 30일)
So, To give a tiny bit of context: I am working on an interacting multi-model Kalmanfilter for event-detection.
The system has 8 state-variables, and I use 3 models. Therefore, My EEC matrix P is an (8x8x3) matrix. The other variable is a weighting matrix W (2D) of size (3x3).
What do I want to do? --> I want to update P(:,:,i) for all 3 models, so that the 8x8 values of that particular model are scaled by the same weight. I currently implemented this as 2 nested for-loops:
for i=1:Nmodel
for j=1:Nmodel
Pmix(:,:,i) = Pmix(:,:,i) + Wmix(j,i)*(Ppost(:,:,j)); ... % Mixed EECs as weighted average
end
end
So to be clear: If
W = [W11, W12, W13; W21, W22, W23; W31,W32,W33], the following multiplications need to happen:
P(:,:,1) = W11*P(:,:,1) + W21*P(:,:,2) + W31*P(:,:3);
P(:,:,2) = W12*P(:,:,1) + W22*P(:,:,2) + W32*P(:,:3);
P(:,:,3) = W13*P(:,:,1) + W23*P(:,:,2) + W33*P(:,:3);
Please let me know whether it is possible to get rid of the for-loops and only use matrix multiplications?
This would hopefully speed up the Real-Time implementation. Thanks in Advance!
NOTE: The solution has to work in MATLAB r2015a due to contraints with regards to implementation on RT control system. Regards, Ricardo

채택된 답변

Matt J
Matt J 2017년 8월 4일
편집: Matt J 2017년 8월 4일
Yes, it's pretty easy to formulate it as as a matrix multiplication (plus some reshaping)
P=reshape(P,[],3)*W.';
P=reshape(P,8,8,3);
  댓글 수: 2
Ricardo Shousha
Ricardo Shousha 2017년 8월 4일
Thanks a lot for your help. I think you meant just W and not its transpose, as the non-transposed W yields the "correct" multiplications. The only thing still missing is that the Previous P has to be added, but I think I can work that out myself.
Thanks again for showing me this reshape method.
Ricardo

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by