How to index index in the third dimension?
조회 수: 6 (최근 30일)
이전 댓글 표시
To debug the code in a simpler way it would also be good if I saved all the information as tensors, that is, putting the index "s" in the third dimension and leaving the others free. With this, I want to see all the sigma points, estimated variances, etc. How could you carry out this process? The following is part of the code where I want to apply this indexing.
x(:,1) = [208701300 326*10e6 217*10e6 44000000 22000000 lambdah]';
P(:,:,1) = 1e6*eye(6);
n = 6;
m = 3;
steps=38;
fXis = zeros(n, 2*n+1);
hXis = zeros(m, 2*n+1);
x_pred(:,1) = x;
P_pred(:,:,1) = P;
Pxz(:,:,2*n+1) = zeros(n, m);
for s=2:steps
[Xis, W] = SigmaPoints(x_pred(:,s-1),P_pred(:,:,s-1),1);
for k = 1:2*n+1
fXis(:, k) = f(Xis(:,k));
end
[xp, Pp] = UT(fXis, W, Q);
for k = 1:2*n+1
hXis(:, k) = h(Xis(:,k));
end
[zp, Pz(:,:,s)] = UT(hXis, W, R);
Pxz(:,:,s)=0;
for k = 1:2*n+1
Pxz(:,:,s) = Pxz(:,:,s)+ W(k)*((fXis(:,k) - xp)*(hXis(:,k) - zp)');
end
K(:,:,s) = Pxz(:,:,s)/Pz(:,:,s);
x_pred(:,:,s)= xp + K(:,:,s)*(y_total(s,:)' - zp);
P_pred(:,:,s)= Pp - K(:,:,s)*Pz(:,:,s)*K(:,:,s)';
end
Many thanks for any help!
댓글 수: 0
답변 (1개)
sanidhyak
2025년 6월 4일
편집: sanidhyak
2025년 6월 4일
I understand that you are trying to debug your code more easily by saving all the intermediate data using the third dimension for the time step index "s".
When reviewing the code you provided, I noticed that variables like "fXis", "hXis", "x_pred", and "P_pred" are being overwritten at each step. To resolve this and allow inspection of all values across steps, you should modify your code to add the third dimension to these variables, using "s" as the time-step index. This will enable you to retain all the information for each time step, allowing effective debugging.
Kindly refer to the following updated code where the third dimension indexing has been applied:
fXis = zeros(n, 2*n+1, steps);
hXis = zeros(m, 2*n+1, steps);
x_pred = zeros(n, 1, steps);
P_pred = zeros(n, n, steps);
Pz = zeros(m, m, steps);
Pxz = zeros(n, m, steps);
K = zeros(n, m, steps);
x_pred(:,1,1) = x(:,1);
P_pred(:,:,1) = P(:,:,1);
for s = 2:steps
[Xis, W] = SigmaPoints(x_pred(:,1,s-1), P_pred(:,:,s-1), 1);
for k = 1:2*n+1
fXis(:, k, s) = f(Xis(:,k));
end
[xp, Pp] = UT(fXis(:,:,s), W, Q);
for k = 1:2*n+1
hXis(:, k, s) = h(Xis(:,k));
end
[zp, Pz(:,:,s)] = UT(hXis(:,:,s), W, R);
Pxz(:,:,s) = zeros(n, m);
for k = 1:2*n+1
Pxz(:,:,s) = Pxz(:,:,s) + W(k) * ((fXis(:,k,s) - xp) * (hXis(:,k,s) - zp)');
end
K(:,:,s) = Pxz(:,:,s) / Pz(:,:,s);
x_pred(:,1,s) = xp + K(:,:,s) * (y_total(s,:)' - zp);
P_pred(:,:,s) = Pp - K(:,:,s) * Pz(:,:,s) * K(:,:,s)';
end
With this adjustment, all relevant matrices are stored per time step, providing a comprehensive view for analysis and debugging.
For further reference on "3D array indexing" in MATLAB, kindly refer to the below documentation:
I hope this helps!
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!