フィルターのクリア

"Index in position 2 exceeds array bounds. Index must not exceed 401" Error. Can someone explain to me why here an error appears?

1 回表示 (過去 30 日間)
Edfred
Edfred 2023 年 4 月 26 日
コメント済み: albara 2023 年 4 月 26 日
x_exact = randn(size(A,1),L+1); u_exact = randn(size(B,2),L+1);
for k = 1:L
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 26 日
In which line does the error occur?
What are the sizes of variables x_exact, u_exact, A, B and C and what is the value of L?
Edfred
Edfred 2023 年 4 月 26 日
In the line: x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);

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

回答 (2 件)

albara
albara 2023 年 4 月 26 日
The error "Index in position 2 exceeds array bounds. Index must not exceed 401" occurs because the size of the x_exact array is not sufficient to store all the values being assigned to it in the loop. Specifically, the loop runs L times, with k taking values from 1 to L. In the second line of the loop, the code tries to assign values to x_exact(:,k+1), which means that on the last iteration of the loop, k takes the value L and x_exact(:,L+1) is accessed. However, x_exact has only L+1 columns, so the index L+1 exceeds the array bounds and causes an error.
To fix this error, you need to increase the size of the x_exact array to have at least L+1 columns. One way to do this is to initialize x_exact with zeros instead of randn and specify the size to be size(A,1),L+1 before the loop:
scss
x_exact = zeros(size(A,1),L+1);
u_exact = randn(size(B,2),L+1);
for k = 1:L
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end
This will ensure that x_exact has the correct size to store all the values being assigned to it in the loop, and the error should be resolved.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
  2 件のコメント
Edfred
Edfred 2023 年 4 月 26 日
Thank you very much for your detailed answer :)
Does that mean that if I intialize x_exact with randn and the same dimensions as mentioned it does not work and that I can run it just with zeros as initialization?
albara
albara 2023 年 4 月 26 日
You're welcome!
If you initialize x_exact with randn and the same dimensions as mentioned, the code will run without errors as long as L is not too large, because the size of x_exact will be sufficient to store all the values being assigned to it in the loop. However, the initial values in x_exact will be random and may not satisfy any initial conditions or constraints that the system may have. Therefore, it is often better to initialize x_exact with a known or desired initial state, such as all zeros or some other value that is appropriate for the specific system being simulated.
In this case, initializing x_exact with zeros is a good choice because it ensures that the initial state is known and satisfies any initial conditions or constraints. Additionally, it has the same size as u_exact and makes it easier to combine x_exact and u_exact into a single matrix for later analysis, if desired.

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


VBBV
VBBV 2023 年 4 月 26 日
編集済み: VBBV 2023 年 4 月 26 日
I guess, L may be a vector or array which keeps changing in your program. Try the below
x_exact = randn(size(A,1),length(L)+1);
u_exact = randn(size(B,2),length(L)+1);
%
for k = 1:length(L)
%-->>
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by