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

2 次查看(过去 30 天)
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 个)

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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by